JQuery - check if the value of the selection window has changed from another JS code

I know that you can use the onChange method, but onChange does not start if I change the value by code, for example:

<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 message to be “changed” to a popup ... Is this possible?

+3
source share
2 answers

You can try to trigger the event changemanually, for example:

$('#selectBox').val('3').change();
+6
source

You can use the jQuery change event and then call it when you change it from code, for example:

$(document).ready(function() {
  $('#selectBox').change(function() {
    alert("changed")
  });

  $('#selectBox').val('3');
  $('#selectBox').change(); // this will  trigger change event
});
+4
source

All Articles