Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I programmatically change a select so that it fires its onchange behavior?

This doesn't seem to be working:

<select id="mySel" onchange="alert('foo')">
    <option value="a">a</option>
    <option value="b">b</option>
</select>

<script>
dojo.byId('mySel').value = 'b'; // select changes, but nothing is alerted
</script>

(I'm using dojo, but that doesn't really matter.)

like image 268
sprugman Avatar asked Aug 03 '09 21:08

sprugman


1 Answers

The 'onchange' name is a little misleading unless you understand that a change event and a value being changed aren't the same thing. A change event occurs when the user changes the value in the browser. I believe you can fire the event manually by calling dojo.byId('mySel').onchange() after you programmatically change the value, though. (You might need to actually define a function that calls alert, though. I haven't done this myself.)

like image 191
Meredith L. Patterson Avatar answered Sep 21 '22 07:09

Meredith L. Patterson