Trigger change event of dropdown using jQuery

With jQuery you can easily update the value of a select dropdown box, but this won't automatically trigger the 'change' event in the same way it would if you selected it manually. This code-snippet will change the value, and then trigger the event to run any associated code:

HTML

<select id='languages'>
    <option value='css'>CSS</option>
    <option value='html'>HTML</option>
    <option value='js'>Javascript</option>
</select>

jQuery

Update the the select element by value and then use the 'trigger' method to call the change event:

$('#languages').val('js');
$('#languages').trigger('change');

Sign Up
comments powered by Disqus
Sign Up

Popular Tags

350x250

Need a web developer?

If you'd like to work with code synthesis on your next project get in touch via the contact page.