Working with an input the following behaviour has been discovered. When the label is attached to the input with for
attribute two click events will happen. Can someone please explain the behaviour of this "anomaly".
And if there a way to prevent the click happening on input if only the label was clicked, of course besides removing the for
attribute.
document.addEventListener(`click`, onClick)
var counter = 0;
function onClick(event) {
++counter
console.log(counter)
}
label {
display: block;
background-color: lightgrey;
margin: 5px 0 5px;
}
<input id='input'>
<label for='input'>I belong to input</label>
<label>I belong to no one</label>