Sunday, 27 November 2016

javascript - what's the equivalent of jquery's 'trigger' method without jquery?



what's the equivalent of jQuery's trigger method without jQuery?



e.g., how do i do something like $('.blah').trigger('click'); without jQuery?



Answer



For some value of "a very shoddy equivalent":



var button = document.getElementById("thebutton")
button.click()


Not all browsers (e.g. Firefox!) allow simulated events this way! Using onclick() only works if the event is in-line, etc.



Please see the jQuery source and search for "trigger:" (first match) to see all the icky stuff done to "make it work" (a good bit of it is just munging around the jQuery internals, other frameworks make have much simpler examples).




Happy coding.


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...