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?

Alex
  • 66,732
  • 177
  • 439
  • 641
  • I remember that this situation is messy and remember people saying to avoid it – qwertymk Jan 17 '11 at 04:19
  • possible duplicate of [jQuery selector for inputs with square brackets in the name attribute](http://stackoverflow.com/questions/2364982/jquery-selector-for-inputs-with-square-brackets-in-the-name-attribute) – Phrogz Jan 17 '11 at 04:19
  • You might as well add a class on the element. It would be faster to evaluate. – Christian Joudrey Jan 17 '11 at 04: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. – Phrogz Jan 17 '11 at 04:21
  • BTW, did you try this? My tests show that it just works, quoted or not, with the latest version of jQuery. – Phrogz Jan 17 '11 at 04:24
  • yes, and I get a 'undefined' object – Alex Jan 17 '11 at 04:32
  • actually you're right. the problem was elsewhere, thanks – Alex Jan 17 '11 at 05:00

3 Answers3

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()...
Community
  • 1
  • 1
qwertymk
  • 34,200
  • 28
  • 121
  • 184
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/

Reigel Gallarde
  • 64,198
  • 21
  • 121
  • 139
  • tx. is there a function that can automatically escape a string? because that attribute is retrieved dynamically in my case. – Alex Jan 17 '11 at 04:23
  • 1
    @Alex, well, regex is the best approach... look at qwertymk's [answer](http://stackoverflow.com/questions/4709986/jquery-select-a-element-by-the-name-attribute/4710011#4710011) – Reigel Gallarde Jan 17 '11 at 04:26
  • Instead of racing to the answer, let's race to close duplicate questions and help keep the clutter to a minimum :p – Phrogz Jan 17 '11 at 04:28
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;
})...
PleaseStand
  • 31,641
  • 6
  • 68
  • 95