Take the 2-minute tour ×

So, i'm writing a plugin using js with jQuery, that will replace all buttons (input type=submit and <button>) on my site's pages with nice buttons that look something like <a href='#'><span class='label'>Ok</span><i></i></span>

At the very beginning i ran into trouble: some of my buttons don't just submit the form - some are hooked with jquery.validation plug-in, some are used to do ajax calls.

So the first idea was to save all click handlers associated with the original button (input type=submit) somewhere and then hook them to the new button (<a href...). But I haven't found a way to get references to the click handlers, so here's the first question:

Is there a way to get references to all event handlers, associated with an element?

The second intention was to hide original button instead of replacing it with a new one and emulate a click on it when a user presses the hyperlink on a new one. This idea was ruined on the stage of implementing. When i use $(myButtonSelector).click(), the form gets submitted, ignoring the jquery.validation click handlers, associated with it. $(myButtonSelector).trigger('click') works the same way - the form gets submitted, ignoring the validation. $(myButtonSelector).triggerHandler('click') doesn't work at all.

How to execute the existing click handlers?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

You can try just copying over the events.

  var events = $(this).data('events');

  // Copied over from jQuery.fn.clone
  var newElement = // 'a' wrapper?
  for ( var type in events ) {
    for ( var handler in events[ type ] ) {
      jQuery.event.add( newElement, type, events[ type ][ handler ], events[ type ][ handler ].data );
share|improve this answer
Wow, thank you, i'll try that. Could you please tell me where the 'events' data field is documented? I really cannot understand, how jQuery can resolve references to event handlers. –  DataGreed Nov 25 '09 at 23:04
git clone git://github.com/jquery/jquery.git - best documentation there is. –  Corey Hart Nov 26 '09 at 10:22
jQuery.event.add didn't work, but jQuery.bind() method does it's job. I also have to mind, that button may have onclick attribute, that is not in the data set object and that it can have no handlers at all - in this case, you have to bind submit() to it. Thanks. –  DataGreed Nov 26 '09 at 13:10

Your Answer


