I recently had to pull together some jQuery to get the value of what was selected in a select box. Its pretty easy to do with jQuery. There are probably a few ways to accomplish this, I chose to use the change event which detects when something is changed.

Here is the html code for the select:

<select id="my_select" name="my_select_box">
<option value="yes">yes</option>
<option value="no">no</option>
</select>

And here is the jQuery snippet for getting the value of whichever you have selected:

$('#my_select').change(function() {
   // assign the value to a variable, so you can test to see if it is working
    var selectVal = $('#my_select :selected').val();
    alert(selectVal);
});

And there you have it, pretty simple! I will be posting more of these little helpful jQuery snippets. Stay Tuned!

No Responses to “jQuery Tips: How to get value of selected option in Select Box”




XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

By submitting a comment here you grant Jake Rutter – Front-End Developer and Designer, CSS, JavaScript and jQuery, PHP, Wordpress, Expression Engine, Magento a perpetual license to reproduce your words and name/web site in attribution. Inappropriate comments will be removed at admin's discretion.