code: function( element, type, bubbles, cancelable )
      {
          if( document.createEvent ) {
              var event = document.createEvent('Event');
              event.initEvent( type, bubbles !== undefined ? bubbles : true, cancelable !== undefined ? cancelable : false );
              element.dispatchEvent(event);
          }
          else if( document.createEventObject ) { //IE
              var event = document.createEventObject();
              element.fireEvent( 'on' + type, event );
          }
          else if( typeof(element['on' + type]) == 'function' )
              element['on' + type]();
      }
docs: http://www.cristinawithout.com/content/function-trigger-events-javascript