2

I know you can do this with [name] but the problem is that my input name attribute contains square brackets [] inside :(

var thename = 'blah[blah][]'; // <- this value is dynamic
$("*[name='"+thename+"']").each()...

Is there any other method I could select this element by the name field?

7
  • I remember that this situation is messy and remember people saying to avoid it Commented Jan 17, 2011 at 4:19
  • possible duplicate of jQuery selector for inputs with square brackets in the name attribute Commented Jan 17, 2011 at 4:19
  • You might as well add a class on the element. It would be faster to evaluate. Commented Jan 17, 2011 at 4:20
  • For future reference (this is intended to be helpful, not snarky): If you had included a more rigorous and exact summary of your question in the title, you very likely would have seen the exact duplicate question listed. This would have saved you the time of even writing your question, and jumped straight to the answer. Commented Jan 17, 2011 at 4:21
  • BTW, did you try this? My tests show that it just works, quoted or not, with the latest version of jQuery. Commented Jan 17, 2011 at 4:24

3 Answers 3

2

You have to escape them, you can do this with a regex replace

var thename = 'blah[blah][]'; // <- this value is dynamic
$("*[name='"+thename.replace(/\[/g, '\\\\[').replace(/\]/g, '\\\\]')+"']").each()...

Or to make a function

function esc(a) { return a.replace(/\[/g, '\\\\[').replace(/\]/g, '\\\\]'); }
var thename = 'blah[blah][]'; // <- this value is dynamic
$("*[name='"+esc(thename)+"']").each()...
Sign up to request clarification or add additional context in comments.

3 Comments

you have to use double backslash.. :)
Thanks just wanted to keep you on your toes
tx, but I just discovered that the problem wasn't [], it was a bad (:not) selector that I added after :)
1
If you wish to use any of the meta-characters 
( such as !"#$%&'()*+,./:;?@[\]^`{|}~ ) as a 
literal part of a name, you must escape the 
character with two backslashes: \\. 
For example, if you have an an element with 
id="foo.bar", you can use the selector $("#foo\\.bar"). 

quoted from http://api.jquery.com/category/selectors/

3 Comments

tx. is there a function that can automatically escape a string? because that attribute is retrieved dynamically in my case.
@Alex, well, regex is the best approach... look at qwertymk's answer
Instead of racing to the answer, let's race to close duplicate questions and help keep the clutter to a minimum :p
1

First try using double quotes within the attribute selector:

$('*[name="'+thename+'"]').each()...

If that doesn't work, you can use the .filter() method, taking advantage of direct DOM access:

$('input').filter(function() {
    return this.name == thename;
})...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.