Prevent onclick event on a button from firing when hitting Enter in a different box Prevent onclick event on a button from firing when hitting Enter in a different box google-chrome google-chrome

Prevent onclick event on a button from firing when hitting Enter in a different box


Its default HTML behaviour. All the buttons inside a form are of type="submit". On pressing enter, the most recent button of the form is clicked and their handlers are called, by default. To fix this, the buttons are created with type="button".

console.log(document.querySelector("#a").type)console.log(document.querySelector("#b").type)console.log(document.querySelector("#c").type)
<form>    <input type="text">    <button id="a" onclick="console.log('a')">Submit type</button>    <button id="b" onclick="console.log('b')" type="button">Button type</button>    <input id="c" type="submit" value="Input Submit type"></form>