I have always believed that the default behavior of hitting the 'Return' key was to submit the form. But it seems to be more complex: if there is a nearby button, the 'Return' key instead seems to simulate a 'click' event on that button, instead of submitting the form. Here's an example:
document.querySelector('form').onsubmit = ev => {
ev.preventDefault();
document.querySelector('#result').innerText
= ev.target.elements.text.value;
};
document.querySelector('#troublesome').onclick =
ev => {
ev.preventDefault();
ev.target.innerText = Math.random();
};
<form>
<input type="text" name='text' value='something' />
<button id="troublesome">Trouble</button>
<button>Submit</button>
</form>
<div id="result">not submitted</div>
Above, when you hit return in the <input>
element, the browser chooses to click the "Troublesome" button. But why? What exactly is happening here? And where can I read it in the specs?