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.
I was looking for getting val by internal field name instead of ID and came from google to this post which help but did not find the solution I need, but I got the solution and here it is:
So this might help somebody looking for selected value with field internal name instead of using long id for SharePoint lists:
While "looping" through dynamically created select elements with a .each(function()...):
$("option:selected").text();
and$(this + " option:selected").text()
did not return the selected option text - instead it was null.But Peter Mortensen's solution worked:
I do not know why the usual way does not succeed in a
.each()
(probably my own mistake), but thank you, Peter. I know that wasn't the original question, but am mentioning it "for newbies coming through Google."I would have started with
$('#list option:selected").each()
except I needed to grab stuff from the select element as well.