Swesh
Swesh

Reputation: 150

How to get all select elements having their with multiple selection criteria, jquery?

How to get all dropdowns having on the basis of multiple attributes name and value. $('input[name="ABC"][value="-1"]') this expression seems to work, but for drop downs this doesn't seems working.

<select name="department"  />
<option value="-1" selected="selected">Select One</option>
<option value="0">1</option>
<option value="1">2</option>
<option value="2">3</option>
</select>
alert($("select[name=department][value=-1]").length);

gives 0 in alert box.

refer to this fiddle. http://jsfiddle.net/8QBH7/

Upvotes: 0

Views: 62

Answers (2)

megawac
megawac

Reputation: 11363

I believe this is the query you're looking for:

$("select[name='department'] option[value=-1]")

See fiddle demo

Edit based on your feedback I think you want to use the selected pseudo class. Fiddle

alert($("select[name=department] :selected").val());
$("select").on("change",function(){
    alert($("select[name=department] :selected").val());
});

Upvotes: 1

skos
skos

Reputation: 4222

alert($("select[name=department] option[value=-1]").length);

Upvotes: 1

Related Questions