artnikpro
artnikpro

Reputation: 5869

jQuery change and select

When I change select manually the $("select").change event triggers as expected. But it does not triggers if I change select via JavaScript

Fiddle: http://jsfiddle.net/72Lsw/

Why is this happens and how to avoid this?

Upvotes: 2

Views: 183

Answers (2)

Mathew Thompson
Mathew Thompson

Reputation: 56429

Setting the value of the select via jQuery won't invoke the event:

$("select").val(rand_int);

You'll need to invoke it yourself, after setting it in jQuery, like so:

$("select").val(rand_int).change();

Upvotes: 2

techfoobar
techfoobar

Reputation: 66663

You will need to trigger the change event via code using:

$("select").val(rand_int);

// trigger change
$("select").trigger('change'); 

Upvotes: 2

Related Questions