tags:

views:

38

answers:

2

I have a button on a form and when it's clicked I wish to find the selectedvalue from a select element using JQuery. Can anyone help?

+2  A: 
var selectedValue = $('select#id_of_your_select').val();
Darin Dimitrov
A: 

let's assume this html code:

<form id ='formId'>
<select id='selectBox' name='selectBox'>
    <option value='1'>option 1</option>
    <option value='2'>option 2</option>
</select>
<input type='button' value='go' id='goButton' />

the jquery code it will be this:

$(document).ready( function(){
    $("#formId").submit(function(){
        return false; // disable the regular submit
    });
    $("#goButton").click( function(){
        var desiredValue = $("#selectBox").val();
        alert( desiredValue);
    });
});

another way is to detect the change as it happens:

$(document).ready( function(){
    $("#selectBox").change( function(){
        var desiredValue = $(this).val();
        alert( desiredValue);
    });
});
Elzo Valugi