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:
$("#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);
}
}
Try to unbind the click event from the element and then specify your own.
Edit:
added removeAttr, thanks @gdoron
You should be able to unbind the click method, and then rebind what you want to do instead:
Edit: try this instead to override the onclick attribute:
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:
See fiddle
Notes:
prependHandler
, we use.call(this)
to ensure thatthis
(a reference to the target element) is available asthis
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..get(0)
internally limitsprependHandler
to working on the first selected element.prependHandler
as a jQuery plugin to overcome the above limitation but it will work fine as it is on a single element.