-1

Please have a look at the following snippet https://jsfiddle.net/simbunch/1kvfg7h0/3/

As you can see, the script is using pure JS. As it turns out, onsubmit="return false" does not work when the form is selected using pure JS, whereas if I change the submit code to $('#myform').submit(), then the onsubmit works.

My problem is, I cannot edit the script part (CMS environment). What I want to do is to basically stop the form from submitting. Thanks in advance!

-edit- The issue seems to be related to Firefox. The snippet works as expected in Chrome.

Mike Feng
  • 803
  • 2
  • 9
  • 19

2 Answers2

2

Returning false is a JQuery convention. It doesn't work in vanilla Javascript.

To achieve a similar effect you need to use the "e" argument that's passed into your event handler, and call either its stopPropagation or preventDefault methods ... but since there are no arguments at all in an onsubmit attribute I wouldn't have thought it was even possible from such an attribute.

However, this answer (Access event to call preventdefault from custom function originating from onclick attribute of tag) suggests a way to do it. I haven't personally tried it so YMMV.

machineghost
  • 33,529
  • 30
  • 159
  • 234
1

You can only execute functions in onsubmit attribute of the form tag also returning false won't do anything. You can use event.preventDefault() here.

try something like this below

<form id="myform" onsubmit="function (e) { e.preventDefault(); } ">
fullmetal
  • 414
  • 3
  • 11
  • `return false;` does work, but it turns out the problem is with Firefox. Plug your code into the jsfiddle and try running it on Firefox, it'll not work as well: https://jsfiddle.net/simbunch/1kvfg7h0/10/ – Mike Feng May 24 '19 at 17:05