<select id="facetList" style="width:120px;" class="text ui-widget-content ui-corner-all">
<?php
foreach($claAddArray as $k => $v)
{
echo "<option value=\"$k\">$v</option>";
}
?>
</select>
$("#btnSubmit").click(function() {
var $fId = ?????????????????
});
2 Answers
jQuery's .val() will return the currently selected option's value if run on the select list itself. In your case $("#facetList").val();
use $("#facetList option:selected").text(); for the text of the option tag.
4 Comments
ysth
The OP also asked for the label.
Mark Baijens
and
$("#facetList").children(":selected").html(); for $vDampeS8N
Mine's shorter. :) But yours is good too. (Wow.. I can't believe I said that...)
Mark Baijens
Yeah text is also a better function to use as html :). I'm wanna go back sleeping...
$("#facetList :selected").text(); would be even shorter.document.getElementById('facetList').options[document.getElementById('facetList').selectedIndex].value
4 Comments
DampeS8N
works too, but jQuery is already being used so using .val() is a better choice.
Ahmad Farid
Damp, I tried your solution as well. It's obviously shorter and easier, but why do you say it's better? Also, what shall I change if I want to get the value or key instead?
seriousdev
So wanna get
$v? I think this should do the trick: document.getElementById('facetList').options[document.getElementById('facetList').selectedIndex].innerHTMLDampeS8N
It is shorter, and easier to read, and is how the framework intends for you to do it. Therefore it is better. However if you want other information, you'll need to go about it differently. Once you have the value, you can use it to 'look up' which object has that value. Raw JS is more powerful in that regard, but as you see, much harder to read.