jQuery selector for inputs with square brackets in the name attribute

jQuery selector for inputs with square brackets in the name attribute

I’m trying to select this element which has square brackets in the name attribute:

I’ve tried this (which doesn’t work):

and neither does this:

or this:

EDIT: As some of you have pointed out, $(‘input[inputName=someValue]’) would never work. What I was trying to do was: $(‘input[name=inputName][value=someValue]’). (But with [] in the name attribute).


Solution 1:

Per the jQuery documentation, try this:


However, I’m not sure that’s the right syntax for your selector. You probably want:


Solution 2:

You can use backslash to quote “funny” characters in your jQuery selectors:


For attribute values, you can use quotes:


Now, I’m a little confused by your example; what exactly does your HTML look like? Where does the string “inputName” show up, in particular?

edit fixed bogosity; thanks @Dancrumb

Solution 3:

The attribute selector syntax is [name=value] where name is the attribute name and value is the attribute value.

Related:  CodeIgniter - How to return Json response from controller

So if you want to select all input elements with the attribute name having the value inputName[]:


And if you want to check for two attributes (here: name and value):


Solution 4:

If the selector is contained within a variable, the code below may be helpful:

selector_name = $this.attr('name');
//selector_name = users[0][first:name]

escaped_selector_name = selector_name.replace(/(:|\.|\[|\])/g,'\\$1');
//escaped_selector_name = users\\[0\\]\\[first\\:name\\]

In this case we prefix all special characters with double backslash.