0

I'm working on a complex form with various input fields in different tabs. To handle these tabs and put DIV's in front for example I'm using javascript click events. For example to set the first tab when the page builds I'm using:

document.getElementById("defaultOpen").click();

I have a FORM around the whole form and this gets submitted whenever the javascript click event gets fired. What I've tried so far: - I replace the submit button with a normal button. Unfortunately no impact. - I added an onsubmit="return mySubmitFunction(event)" event which returns false and prevents the form from being sent.

Is someone familiar with this issue click events/form submission and knows the best way how to solve it and could push me in the right direction?

Thanks,

Lukas

Lukas
  • 3
  • 1
  • not sure to understand all but you can try to add `type=button` as ` – Yukulélé Mar 08 '20 at 10:28
  • I already tried it with the following submit button: `` but somehow it still fires the click event – Lukas Mar 08 '20 at 10:36
  • 1
    You can use `event.preventDefault();` in an event listener to stop it from being submitted. – Federico Moretti Mar 08 '20 at 10:50
  • Hi all, this works EXCELLENT and was directing me in the right direction - THANK YOU! So what I did: I added the `event.preventDefault();` to the form handling functions. That prevents a form submission form. That's it - everything works as expected! :) – Lukas Mar 08 '20 at 11:14
  • ps. how can I close this question? Do I have to delete it to prevent anyone from spending time on that or can someone mark it as solved? – Lukas Mar 08 '20 at 11:18
  • Yes, it does - question solved! :) But I don't find out how to close it :/ – Lukas Mar 08 '20 at 11:47

1 Answers1

0

The below code avoid you from submission when you click the button.

$('button').click(function(e){
       e.preventDefault();

        // rest of your code

    });
Murtaza JAFARI
  • 674
  • 7
  • 10