user789122
user789122

Reputation: 480

jQuery selector contains brackets

I have a select list with the following markup:

<select name="dropdown[users]">
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">2</option>
</select>

I want to get the value of the select list option.

how is this done is jQuery?

I have tried:

$('select[name="dropdown[users]"]');

This doesn't seem to work

Any ideas?

Upvotes: 4

Views: 4320

Answers (2)

Yusaf Khaliq
Yusaf Khaliq

Reputation: 3393

to obtain the value you must use the .val() method

http://jsfiddle.net/xPnvE/1/

$('select[name="dropdown[users]"]').change(function(){
  alert($(this).val());
});

Upvotes: 0

Rory McCrossan
Rory McCrossan

Reputation: 337560

Use val():

var selectValue = $('select[name="dropdown[users]"]').val();

Example fiddle

The fact the name has [] in it is not a factor as you have enclosed the attribute value in quotes.

If you removed the quotes, you would need to escape the braces with \\, like this:

var selectValue = $('select[name=dropdown\\[users\\]]').val();

Upvotes: 7

Related Questions