Return to Snippet

Revision: 22001
at December 28, 2009 12:49 by bryanlyman


Updated Code
function FireOnChange(element) {
  if (element.onchange) {
      var e = null;
      if (document.createEventObject) {
          //ie
          e = document.createEventObject();                    
          element.fireEvent('onchange', e);
      }
      else {
          //others
          e = document.createEvent('HTMLEvents');
          e.initEvent('change', true, true);
          element.dispatchEvent(e);
      }
   }
}

Revision: 22000
at December 28, 2009 12:43 by bryanlyman


Initial Code
function FireOnChange(element) {
  if (element.onchange) {
      var e = null;
      if (document.createEventObject) {
          e = document.createEventObject();                    
          element.fireEvent('onchange', e);
      }
      else {
          e = document.createEvent('HTMLEvents');
          e.initEvent('change', true, true);
          element.dispatchEvent(e);
      }
   }
}

Initial URL

                                

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

Initial Title
Cross-Browser event triggering

Initial Tags
javascript, event, browser

Initial Language
JavaScript