Inline onclick handler preventing catch form’s submit

I have the following inline code that I can’t change due is outputted by a foreigner plugin. Contain a onclick handler that trigger the submit event:

<input type='button' id='gform_next_button_12_188' 
class='button gform_next_button' value='Next' tabindex='3' 
onclick='jQuery("#gform_target_page_number_12").val("2"); jQuery("#gform_12").trigger("submit",[true]); '/> 

I want to run some routines before submit the form and I tried this:

Read More
$("#gform_12").submit(function(e){

    // this code is not running ...

});

That it is failing, ie, my code is not running when form is submitted . Any suggest?

Final solution based on answers given

var inline_onclick_code_on_next = $(".gform_next_button").first().attr("onclick");
$(".gform_next_button").removeAttr("onclick");
$(".gform_next_button").bind("click", function() {
    myClass.autosave_on_click(inline_onclick_code_on_next);
});

myClass = {
    errorMessage : "",
    autosave_on_click: function (inline_onclick_code){
        my_gravity_form_saving_running = 1;
        myClass.saveData(); // this code must put my_gravity_form_saving_running = 0 when all data be saved
        countdown_max_loop = 10; // seconds
        var countdown = window.setInterval(function(){
            if (countdown_max_loop-- == 0 || my_gravity_form_saving_running == 0){
                clearInterval(countdown);
                eval (inline_onclick_code);
            }
        }, 1000);
    }
}

Related posts

Leave a Reply

3 comments

  1. Try to unbind the click event from the element and then specify your own.

    $('#gform_next_button_12_188').unbind('click').removeAttr('onclick');
    $('#gform_next_button_12_188').bind('click', function() {
        // do magic stuff
    
        // do the rest
        jQuery("#gform_target_page_number_12").val("2");
        jQuery("#gform_12").trigger("submit",[true]); 
    });
    

    Edit:

    added removeAttr, thanks @gdoron

  2. You should be able to unbind the click method, and then rebind what you want to do instead:

    $('#gform_next_button_12_188')
       .unbind('click')
       .bind('click', function() {
           // your stuff
           // submit stuff that was there
       });
    

    Edit: try this instead to override the onclick attribute:

    $('#gform_next_button_12_188')
      .attr('onclick', '')
      .bind('click', function() {
          // your stuff
          // submit stuff that was there
      });
    
  3. It would be convenient to be able to prepend your stuff without having to retype (or even to know) the inline stuff.

    The following function will do this:

    $(function(){
        function prependHandler(selector, event, fn) {
            try {
                if (event.split(' ').length > 1) {
                    event = event.split(' ')[0];
                }
                var $el = $(selector),
                    inline_handler = $el.get(0)['on' + event];
                $el.on(event, function() {
                    fn.call(this); //invoke your own magic stuff
                    inline_handler.call(this); //invoke original inline magic stuff
                }).get(0)['on' + event] = null;
                return true;
            }
            catch (e) {
                return false;
            }
        }
    
        //call as follows:
        prependHandler("#gform_next_button_12_188", 'click', function() {
            alert('my functionality'); //specify your own magic stuff here
        });
    });
    

    See fiddle

    Notes:

    • In prependHandler, we use .call(this) to ensure that this (a reference to the target element) is available as this inside the original handler and the prepended handler, in case it’s needed.
    • prependHandler returns true or false to allow you to test whether it was successful or not, in case you need to know.
    • Whereas you could pass in a selector that selects multiple elements, the use of .get(0) internally limits prependHandler to working on the first selected element.
    • With a litle more thought you could frame prependHandler as a jQuery plugin to overcome the above limitation but it will work fine as it is on a single element.