All right, say I have this:
<select id='list'>
<option value='1'>Option A</option>
<option value='2'>Option B</option>
<option value='3'>Option C</option>
</select>
What would the selector look like if I wanted to get "Option B" when I have the value '2'?
Please note that this is not asking how to get the selected text value, but just any one of them, whether selected or not, depending on the value attribute. I tried:
$("#list[value='2']").text();
But it is not working.
Try the following:
The reason why your original snippet wasn't working is because your
OPTION
tags are children to yourSELECT
tag, which has theid
list
.I needed this answer as I was dealing with a dynamically cast object, and the other methods here did not seem to work:
This of course uses the DOM object instead of parsing its HTML with nodeValue, childNodes, etc.
A tip: you can use below code if your value is dynamic:
Or (better style)
As mentioned in jQuery get specific option tag text and placing dynamic variable to the value
I wanted a dynamic version for select multiple that would display what is selected to the right (wish I'd read on and seen
$(this).find
... earlier):Based on the original HTML posted by Paolo I came up with the following.
It has been tested to work on Internet Explorer and Firefox.