'How can I check if a submit button is clicked and not an enter press on a page?

Imagine this piece of html:

​<form>
    <input type="text">
    <input type="submit" id="a">
</form>​​​​​​

and this piece of Javascript

document.getElementById('a').onclick = function (e) {
    alert(e.type);
};​​​​

The alert will always say 'click', even if you press enter in the textfield.

How can I check if the button is really clicked?



Solution 1:[1]

Imagine this piece of html:

<form>
    <input type="text">
    <input type="button" id="a">
</form>

and this piece of Javascript

document.getElementById('a').onclick = function (e) {
    this.form.submit();
};????

No confusion will be possible.

There is almost no reason to use input type="submit" in modern web applications.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Denys Séguret