Tomkarho
Tomkarho

Reputation: 1817

Onsubmit normal behaviour?

Okay so based on how I understood it when you attach onsubmit to a form the code that is set will be run. So in essense the following code:

function init() {
    'use strict';
    document.getElementById('saveForm').onSubmit = saveTodo();
    }

window.onload = init;

Should trigger the saveTodo - function. However I was under the impression that this is done only when the form is submitted. Here's the saveTodo - function:

function saveTodo() {
    'use strict';

    console.log('saveTodo()');
    return false;

}

Yet when I refresh the browser this function is called even though no form submission has been clicked.

Is this normal behaviour for onsubmit?

Here's the html form:

<form id="saveForm" name="saveForm">
      <div>
         <label for="todo">Give a task:</label>
         <input type="text" name="todo" id="todo" />
      </div>
      <div>
         <input type="submit" name="add" id="add" value="Add" />
      </div>
 </form>

Upvotes: 0

Views: 238

Answers (2)

Tomkarho
Tomkarho

Reputation: 1817

Ah it seems the problem was a stupid typo. Instead of using onSubmit the syntax was onsubmit. So the following code worked:

function init() {
    'use strict';

    document.getElementById('saveForm').onsubmit = saveTodo;
    }

Thanks for rps for noticing the parentheses.

Upvotes: 0

user2587132
user2587132

Reputation:

document.getElementById('saveForm').onSubmit = saveTodo;

Upvotes: 1

Related Questions