I have a payment form on my Wordpress page and I would like to run a javascript code, after the form submit.
This is the code I'd like to submit:
ny.track({name: (new URL(window.location.href)).searchParams.get("utm_source"), value: 24.50, unit: "CHF"})
This is what I have on my Wordpress page:
jQuery(".wpf_submit_button").submit(function() {
ny.track({name: (new URL(window.location.href)).searchParams.get("utm_source"), value: 24.50, unit: "CHF"});
});
Unfortunately, this does not seem to work. If I change this to fire on click (eg. jQuery(".wpf_submit_button").click(function...
then it works without any problem, so I'm not sure what I'm doing wrong. Here is the link to a testpage with the code implemented. I am using WPPayForm for the payment form.
Any help is greatly appreciated!
CodePudding user response:
.submit()
is binded to de "Submit" javascript event acording jQuery documentation. You should use your form id instead of the button class as jQuery selector:
jQuery("#wpf_form_id_354").submit(function() {
ny.track({name: (new URL(window.location.href)).searchParams.get("utm_source"), value: 24.50, unit: "CHF"});
});
Check jQuery docs: https://api.jquery.com/submit/
The submit
event is sent to an element when the user is attempting to submit a form. It can only be attached to <form>
elements.