Skip to content

Instantly share code, notes, and snippets.

@StephaneTy-Pro
Created June 2, 2015 12:33
Show Gist options
  • Save StephaneTy-Pro/0c134b8b55c7ff2b9eeb to your computer and use it in GitHub Desktop.
Save StephaneTy-Pro/0c134b8b55c7ff2b9eeb to your computer and use it in GitHub Desktop.
Javasript FireEvent
// 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