Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically firing the onchange event of an HTML select element

Tags:

javascript

How can I fire the onchange event of an HTML Select element by code.

The following only selects a list item but doesn't seem to fire the onchange event?

options[index].selected = true;
like image 969
Terman Avatar asked Feb 25 '23 11:02

Terman


1 Answers

If the event is hoked up directly via onchange, you can invoke the handler by calling that handler, like this:

mySelect.options[index].selected = true;
mySelect.onchange();

...if it's not rigged up that way, then different approaches are appropriate depending on how you're binding, and more information about how your onchange handler(s) are attached would help.

like image 64
Nick Craver Avatar answered Feb 28 '23 11:02

Nick Craver