tags:

views:

31

answers:

2

Hi,

In HTML:

<select name=site[new][fw][version]></select>

In jQuery, I've tried:

var fwvs=$('select[name=site\\[new]\\[fw]\\[version]]')

But fwvs.length return 0. What am I doing wrong?

Thanks

+2  A: 

You should escape both opening ([) and closing (]) brackets. Check jquery selectors docs.

An example

Nikita Rybak
like : var fwvs=$('select[name=site\\[new\\]\\[fw\\]\\[version\\]]')
Haim Evgi
As i commented above, this did not worked for me, I had to use \\\ instead
fabjoa
+2  A: 

Try this:

$('select[name="site[new][fw][version]"]')

The attribute selectors can take arguments as strings, wrapped in quotes (single or double, same as JavaScript). Usually these aren't used, but they are useful in this case.

Working example: http://jsfiddle.net/kobi/32SNS/

Kobi
That doesn't work.
Marko
@Marko Ivanovski - seems to work well for me, on Firefox, Chrome and IE8.
Kobi
@Kobi - my bad :)
Marko
That worked! Also, triple escaping (\\\) worked when double (\\) failed for me. Double works fine when the array has no other arrays in it, i.e., site[new], but after then, it seems triple escaping must be used. But thanks Kobi, your way worked though! I'll mark it as answered as soon as SO let me do so :)
fabjoa
@fabjoa - Thanks. The other method should work as well (you are describing a strange error - I'd expect 2 ` \\ ` or 4 ` \\\\ `, if the JS is in a higher-level string), but this is probably simpler.
Kobi