I'm trying to add event listener to a button inside a form but for whatever reason it doesn't work the way I want it. When on the other hand I move the button out of the form it starts working.
document.getElementById('a').addEventListener('click', () => {
document.getElementById('a').style.background = 'red';
})
document.getElementById('b').addEventListener('click', () => {
document.getElementById('b').style.background = 'red';
})
<form>
<button id="a">test</button>
</form>
<button id="b">test</button>
When I click the button inside the form it turns red for a fraction of second and then goes back to default style. Can someone explain me what causes this behaviour and suggest a solution?