Advertisement
Advertisement


jQuery finding select elements by value


Question

In jQuery, why does $("#myid").val() return the value of a select element but $("select[value='abcd']") does not find a select element that has a value of abcd? And how do I do the latter - find all select elements in a form that have a value of 'abcd'?

EDIT: I managed to retrieve the select element ids with a workaround: I added the select's id as an attribute on all its' options. Then I used: $("option[value=abcd]:selected") to find all selected options with the value of abcd and finally $(this).attr("myAttr") inside .each(function(){ }) to get the id.

Is there a better way of doing this?

2017/06/02
1
0
6/2/2017 6:15:22 AM


you can use following method to get selected value

$("#id option:selected").val();

2017/06/02