user2933671
user2933671

Reputation: 273

Get select option menu in javascript/jquery

I have a select option in a form like this

<select id="name" name="name" onblur="date()">
    <option value="RAN">RAN</option>
    <option value="REE">REE</option>
    <option value="SAM">SAM</option>
    <option value="SEJAl">SEJAl</option>
</select>

I want to get the value of the selected item in the list when the user looses focus. I tried the following script;

function date(){
    var e = document.getElementById("name");    
    var name = e.options[e.selectedIndex].value; 
    console.log(name);
    //do something else....  
}

the console is empty. A simple document.getElementById also doesnt give me the value. so by which method can I get the value?

thanks!

ANSWER FOUND:

$name = $('select[name=name]').val();

Upvotes: 0

Views: 204

Answers (2)

Naveen Kumar Alone
Naveen Kumar Alone

Reputation: 7678

JSBin

function date(){
    var e = document.getElementById("name");    
    alert(e.value); 
}

This will shows value in alert perfectly, when tou select any value ant clicks outside :)

Upvotes: 1

namikiri
namikiri

Reputation: 81

Use e.value, like this:

function date(){
    var e = document.getElementById("name");    
    console.log(e.value); 
}

Upvotes: 1

Related Questions