Javascript click event triggers twice
This happens because of what the HTML spec describes at 4.10.4:
For example, on platforms where clicking a checkbox label checks the checkbox, clicking the
label
in the following snippet could trigger the user agent to run synthetic click activation steps on theinput
element, as if the element itself had been triggered by the user:<label><input type=checkbox name=lost> Lost</label>
On other platforms, the behavior might be just to focus the control, or do nothing.
This means that when a <label>
is clicked, the browser creates a second "synthetic" click event on the associated <input>
element, in order to toggle its state.
The reason divClicked
is triggered twice, is because the first event which comes from the <label>
bubbles up to the <div>
, and also the second, synthetic click event bubbles up to the <div>
.