Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery - Check if a value of a select box changed from another JS code

I know that you can use the "onChange" method, but the onChange doesn't get fired if I change the value by code, like this :

<select id='selectBox' onChange='alert("changed")'>
<option value="1">1</option>
<option value="1">2</option>
<option value="1">3</option>
<select>

<script>
$(document).ready(function() {
$('#selectBox').val('3');  
});
</script>

When the document loads, I would like the alert "changed" to popup... Is this possible?

like image 212
tink01 Avatar asked Jan 24 '26 22:01

tink01


1 Answers

You could try firing the change event manually, like so:

$('#selectBox').val('3').change();
like image 120
Jimmy Sawczuk Avatar answered Jan 26 '26 14:01

Jimmy Sawczuk