Add onSubmit to FORM-tag

  • 1 February 2018
  • 2 replies
  • 5 views

For linking the landing page to our CRM, I have added some javascript code. Now I need to run this code on form submit.

So in the FORM-tag, I need to remove the ACTION and replace it with: onSubmit=“createObject(); return false”

Is it possible to edit the HTML-code?
Can you please advice?

Thank you
Vincent


2 replies

Userlevel 7
Badge +3

Hi @VincentTahon,

You will need to write a bit of JS to accomplish what you want.

You’ll have to pause the form submit action, execute your createObject and release the native form action to complete the form submission.

Hope this points you in the right direction.

Best,
Hristian

Hi Hristian,

Thank you for the advice.
I have tried, but did not succeed in pausing the form-submit action. Do you have an example for that?

These are some things I tried (but unfortunately I am not very skilled in JS):

jQuery(document).ready(function() {
(document).find("form").eq(0).removeAttr("method"); (document).find(“form”).eq(0).removeAttr(“action”);
(document).find("form").eq(0).attr("id","myform123"); (document).find(“form”).eq(0).attr(“onSubmit”,“createObject(); return false”);
initLanding();
// alert(“Page loaded”);
});

After submit I see: “Uncaught TypeError: Cannot read property ‘nodeType’ of null"

Instead of adding the onSubmit attribute, I also tried this:

$(document).on(“submit”, “#myform123”, function(e) {
e.preventDefault();
// alert("submitted myform123”);
createObject();
});

But again, same error occurs…

Kind regards,
Vincent

Reply