Reputation: 1831
I have a page which calls an external library, this library adds a payment form:
<form name="multipay_form" onsubmit="private_payment_send(); return false;">
....
</form>
I can not change any code here. I need to call a function after form is submitted. What I have done is:
jQuery('form[name="multipay_form"]').on('submit', function() {
alert("myfunction");
});
Which works ok, but there is one exception, the method "private_payment_send()", does form validation, I need to know if their function returned true or false, to be able to trigger my function or not.
Is this possible?. I want to avoid doing the validation again on my end, since if they add new field or any new rule I would have to do the same on my code, which is not optimal. Any ideas?
Is there a way to unattach the function from the form through javascript?, in that way I can call private_payment_send() from my function
Upvotes: 1
Views: 117
Reputation: 7591
Try to use done() function in Jquery
.done(function( n ) {
$( "p" ).append( n + " we're done." );
});
Following is Jquery documentation
https://api.jquery.com/deferred.done/
Upvotes: 1
Reputation: 444
<form name="multipay_form" onsubmit="private_payment_send(); return false;">
<button type="submit">test</button>
</form>
document.getElementsByName("multipay_form")[0].setAttribute('onsubmit','');
This will make it so the onsubmit is removed from the form without touching the HTML
Upvotes: 1
Reputation: 129
You can trigger your function only when the called function, here "private_payment_send" has returned true. This can be done like this
<form name="multipay_form" onsubmit="if (private_payment_send()) { alert("myFunction") }; return false;">
...
</form>
If you only want to use the jQuery part, you can completely remove the onSubmit attribute and only assign the submit handler using jQuery
jQuery('form[name="multipay_form"]').on('submit', function() {
if (private_payment_send())
alert("myfunction");
return false;
});
Upvotes: 0