0

onsubmit does not execute in the code snippet below when you press the submit button. It should show 'Submitting' in the javascript console when you press Submit.

function submit(v) {
  console.log('Submitting');
}
<form action="#" onsubmit="submit()">
<input type=submit />
</form>
trinalbadger587
  • 1,905
  • 1
  • 18
  • 36
  • It does, as soon as you choose a different name for your function. (The `form` element has a `submit` method by default.) – CBroe Apr 04 '18 at 09:50

2 Answers2

2

It's a name collision. Name your function something other than submit, it worked for me then.

See below:

function a () {
  console.log('Submitting');
}
<form action="#" onsubmit="a()">
<input type=submit />
</form>
trinalbadger587
  • 1,905
  • 1
  • 18
  • 36
Máté Safranka
  • 4,081
  • 1
  • 10
  • 22
0

submit() is a predefined method in javaScript which submits a form. Just rename it to another name.

function submit2(v) {
  console.log('Submitting');
}
<form action="#" onsubmit="submit2()">
<input type=submit />
</form>
Ali Sheikhpour
  • 10,475
  • 5
  • 41
  • 82