1

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.

0

5 Answers 5

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Cool thanks. PS... I think you meant "var newVal = $(this).val();"
I know this is quite late but if you are using the keyboard (tabbing) to navigate a form and use the up/down arrows to choose from a dropdown list then FireFox (22.0) does not trigger the change event. You need to additionally bind keypress for FireFox. Additional Info: jQuery 1.10.2 using the syntax $('select').on('change', function(){ /* do seomthing */ });
1
$("#mySelect").bind("change", function(event){ ... });

Comments

1

or just use

$("#mySelect").change(function(){
  //do something here
})

Comments

0
$('#mySelect').change(function() {
  alert('Handler for .change() called.');
});

Comments

0
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.