Alexandar Targov
Alexandar Targov

Reputation: 119

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?

Upvotes: 4

Views: 8229

Answers (1)

Caner Akdeniz
Caner Akdeniz

Reputation: 1862

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'){}

Upvotes: 2

Related Questions