Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get select box option value in jQuery

How to get the value of option select box in jQuery if I have the code like this,

<select id='media' name='media'>
    <option value='1'>media1</option>
    <option value='2'>media2</option>
    <option value='3'>media3</option>
</select>

When I code for on change event,

$(document).ready(function()
{
    $("#media").change(function()
    {
        var id=$(this).val();
        var dataString = 'id='+ id;
        alert(id); return false;
    });
});

It gives me media1,media2,media3 instead of 1, 2, 3

How to get the value 1, 2, 3?

like image 939
jayu patel Avatar asked Sep 19 '12 06:09

jayu patel


2 Answers

$('#media').change(function(){
alert($(this).val());
});

this should work the way you want it should.

like image 94
Rahul Avatar answered Sep 27 '22 18:09

Rahul


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

Here id is the id of a select box. I'm sure it will work 100%.

If you want selected text use this one:

$("#id option:selected").text();
like image 35
SureshKumaran Avatar answered Sep 27 '22 17:09

SureshKumaran