I want to know how to trigger the onClick
event of any select
(html bobox element).
I tried to do $('#MySelect').click();
using jQuery
and tried document.getElementById('MySelect').click();
using pure javascript.
But the two don't fire the dropdown event that have the options of the select.
Ps: i have sure that selector $('#MySelect')
exists.
I want to know how to trigger the onClick
event of any select
(html bobox element).
I tried to do $('#MySelect').click();
using jQuery
and tried document.getElementById('MySelect').click();
using pure javascript.
But the two don't fire the dropdown event that have the options of the select.
Ps: i have sure that selector $('#MySelect')
exists.
-
1
$( "#MySelect" ).trigger( "click" );
I think this is what you're looking for – em_ Commented Nov 11, 2013 at 18:24 -
Do you have a defined
onclick
event? Or anonchange
event? – tymeJV Commented Nov 11, 2013 at 18:26 - Using pure javascript this is how you would do it: stackoverflow./questions/7914684/… – megawac Commented Nov 11, 2013 at 18:26
- @ElliotM i tried this just now, with multiple selects, created another page with another new select too, did a lot of tests using this way and i't dont works. – Paulo Roberto Rosa Commented Nov 11, 2013 at 18:29
- @tymeJV no. i did not. – Paulo Roberto Rosa Commented Nov 11, 2013 at 18:29
2 Answers
Reset to default 6are you looking for this
document.getElementById("MySelect").selectedIndex=3;
Programatically triggering a click
event will only run the defined click handler for that element. As you say in the ments, you have no such method defined, therefore no action will take place.