Search code examples
jqueryhtml-selectselected

How to check if an option is selected using jQuery


i'm trying to check if an option i've chosen is selected.

<select id="fontStyle" name="fontStyle">
    <option value="alpha-echo" selected >Alpha Echo</option>
    <option value="anagram">Anagram</option>
</select>

I've used if( $("#fontStyle option").val('anagram')) but it doesn't work. Can you help me?


Solution

  • Here is how to get the value of selected option from select list:

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

    This will give you the alpha-echo for your provided html. And if you want to check if the anagram is selected, you will need to do:

    if($( "#fontStyle option:selected" ).val()=='anagram'){}