Cross-Browser event triggering

/ Published in: JavaScript
Save to your folder(s)

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).

Copy this code and paste it in your HTML
  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


RSS Icon Subscribe to comments

You need to login to post a comment.