6
 <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 2

9

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.

Sign up to request clarification or add additional context in comments.

4 Comments

The OP also asked for the label.
and $("#facetList").children(":selected").html(); for $v
Mine's shorter. :) But yours is good too. (Wow.. I can't believe I said that...)
Yeah text is also a better function to use as html :). I'm wanna go back sleeping... $("#facetList :selected").text(); would be even shorter.
4
document.getElementById('facetList').options[document.getElementById('facetList').selectedIndex].value

4 Comments

works too, but jQuery is already being used so using .val() is a better choice.
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?
So wanna get $v? I think this should do the trick: document.getElementById('facetList').options[document.getElementById('facetList').selectedIndex].innerHTML
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.