views:

115

answers:

1

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?

+1  A: 

You can try just copying over the events.

$('button').each(function(){
  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 );
    }
  }
});
Corey Hart
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
git clone git://github.com/jquery/jquery.git - best documentation there is.
Corey Hart
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