views:

38

answers:

5

Hello,

I want to be able to detect when the user has selected an option from a SELECT element in a form. I know what to do from there, but not sure about how to achieve the initial detection.

<select id="mySelect">
    <option>1</option>
    <option>2</option>
    <option>3</option>
</select>

I would like the solution in JQuery if possible.

Thanks.

+1  A: 
$("#mySelect").bind("change", function(event){ ... });
Josh K
+3  A: 

You probably want the .change() handler, like this:

$("#mySelect").change(function() {
  var newVal = $(this).val();
  //do something
});

.val() gets the current value of the dropdown (as a string), just use that to do whatever you want, e.g. alert("The new value is: " + $(this).val());, etc.

Nick Craver
Cool thanks. PS... I think you meant "var newVal = $(this).val();"
diggersworld
@diggersworld - indeed, thanks :) lysdexic I am.
Nick Craver
A: 
$('#mySelect').change(function() {
  alert('Handler for .change() called.');
});
clifgriffin
A: 

or just use

$("#mySelect").change(function(){
  //do something here
})
melaos
A: 
jQuery('#mySelect').bind('change',function(e){

        //value of selected item
        var _selectedValues=jQuery(this).val();

        //your codes  here
});

i prefer bind over .change(),. click(), etc: as bind is more generic than others.

Praveen Prasad