Posted By

bryanlyman on 12/28/09


Tagged

javascript event crossbrowser browser trigger cross compatible dispatchEvent createevent createeventobject fireevent bryan lyman


Versions (?)

Cross-Browser event triggering


 / Published in: JavaScript
 

A browser friendly way to trigger an event on an element. This is just a simple snippet which doesn\'t really explain the detailed contents of an event object, but it gives an idea of the differences between specific browsers. This function will fire the onChange event for an element passed to it (say...a checkbox).

  1. function FireOnChange(element) {
  2. if (element.onchange) {
  3. var e = null;
  4. if (document.createEventObject) {
  5. //ie
  6. e = document.createEventObject();
  7. element.fireEvent('onchange', e);
  8. }
  9. else {
  10. //others
  11. e = document.createEvent('HTMLEvents');
  12. e.initEvent('change', true, true);
  13. element.dispatchEvent(e);
  14. }
  15. }
  16. }

Report this snippet  

You need to login to post a comment.