tags:

views:

29

answers:

1

I'm trying to access all my select tags with a specific name. I've tried doing this:

$("select[name='blah']") 

and

$("select[name='blah[]']")

but neither are working.

I followed the JQuery documentation that showed it doing it like this for inputs:

$("input[name='newsletter']")

What am I doing wrong?

Thanks

+1  A: 

Update, based on your comment: You should use an attribute-starts-with selector, like this:

$("select[name^='blah[']")

This will select any <select name="blah[........."> which should be specific enough for your needs.

Also, be sure you're running your selector inside a document.ready context like this:

$(function() {
  $("select[name='blah']").doSomething();
});

Without doing this, or after the element in question in the page, the element won't be there to select...so your selector may be just fine, but what it's looking for isn't in the DOM yet. Putting your code in a document.ready handler like I have above ensures the DOM has all the elements ready to select.

Nick Craver
Thanks this worked. Why did I have to use $("select[name^='blah[']") instead of the standard?
2Real
@2Real - The equals selector is an *exact* match, the literal string, but you have a variable in there, so a starts-with or a regex is a more appropriate here :)
Nick Craver
Alright thanks, I understand now.
2Real