views:

68

answers:

1

Hi, I cannot find a solution for that, I need to override a previous setted submit event handler. In my example, I have a iframe page, with their own submit (which can have validation errors, and return false). So, I'm need to do:

$('<iframe></iframe>')
    .load(function() {
       content = $(this).contents()
       form = content.find('form')
       previous_form_submit = .... //This is my question
       form.unbind("submit")
       form.submit(function() {
           var valid = previous_form_submit()
           if (valid) {
             $.post(form.attr('action'), form.serialize(), function(data) {
                //do something
            })
        })
     })

Edit: To be more clearer, the problem is override the event, but without loosing previous handler with validation.

Edit2: I'm using jquery 1.3.2

I'm going to another kind of solution, but I felt curious about how can do that.

Thanks.

+1  A: 

Event handlers are stored in the element's expando data store, accessible by .data(), in this case, .data('events') for event handlers, then you do .submit for bound submit handlers, .click for clicks, etc.


jQuery 1.4+ version:

If the form had one submit handler it would look like this:

var oldHandler = form​​​​​​​​.data("events").submit[0].handler;

Otherwise you need to loop through them, using a $.each() for example:

$.each(form.data("events").submit, function(i, h) {    
  var handler = h.handler;
  //do something with handler
});

jQuery 1.3.x version:

Using a $.each() for example:

$.each(form.data("events").submit, function(i, h) {    
  var handler = h;
  //do something with handler
});

It's best to loop though here, as the indexes aren't cleaned up. A handler may not be the first item, so .submit[0] might be undefined, even if there is a handler.

Nick Craver
Thanks Nick, I'm using jquery 1.3.2.
Hugo
@Hugo - Updated to show the slightly different object layout in 1.3.2 :)
Nick Craver
@Nick - Thanks again, but form.data("events") is returning nothing.Edit: form.data("events") is undefined
Hugo
Mmm.. seems that it doesn't work outside the iframe, in the parent page.
Hugo
@Hugo - You can't cross event handlers in one window with another (not in this way), they're independent cache mechanisms for each jQuery instance...what are you ultimately trying to achieve here? When I mean is that event handler references things in *that* page, you're likely to have all sorts of issues using it in any way in the parent.
Nick Craver
Ok, I'll take a different approach for that. Basically, I'm trying to replace the normal submit of the pages render inside the iframe, with my custom.
Hugo
@Hugo - In that case, why do you need the previous one, if you're going to replace it? :)
Nick Craver
Because the previous one have validation.
Hugo
@Hugo - Do you need your handler to run first? You can just attach an *additional* handler with `.submit(function() { ..myCode... })` and it'll run after the handler that was there before.
Nick Craver
@Nick - I feel soo stupid. ROFL. Thanks!
Hugo