Created
June 2, 2015 12:33
-
-
Save StephaneTy-Pro/0c134b8b55c7ff2b9eeb to your computer and use it in GitHub Desktop.
Javasript FireEvent
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// from http://www.cristinawithout.com/content/function-trigger-events-javascript | |
//It takes 2 parameters: | |
//obj - the object to trigger the event on | |
// evt - the name of the event to trigger (as a string) | |
function fireEvent(obj, evt){ | |
var fireOnThis = obj; | |
if( document.createEvent ) { | |
var evObj = document.createEvent('MouseEvents'); | |
evObj.initEvent( evt, true, false ); | |
fireOnThis.dispatchEvent( evObj ); | |
} | |
else if( document.createEventObject ) { //IE | |
var evObj = document.createEventObject(); | |
fireOnThis.fireEvent( 'on' + evt, evObj ); | |
} | |
} | |
//Example implementation | |
//fireEvent(document.getElementById('mylink'),'click'); //fires click event on element with id="click" | |
//fireEvent(document.getElementById('myinput'), 'blur'); //fire blur event on element with id="blur" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment