Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 237
john Avatar asked Apr 14 '11 05:04

john


1 Answers

event.initMouseEvent("click"...

Here is an example:

function simulateClick(elId) {     var evt;     var el = document.getElementById(elId);     if (document.createEvent) {         evt = document.createEvent("MouseEvents");         evt.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);     }     (evt) ? el.dispatchEvent(evt) : (el.click && el.click()); }  var foo = document.getElementById("hey");  foo.onclick = function () {alert("bar");}  simulateClick("hey"); 
like image 178
Adam Avatar answered Sep 30 '22 04:09

Adam