snipt

Ctrl+h for KB shortcuts

Text only

jQuery : Event on a checkbox called twice

1
2
3
4
5
That's because when you bind a click event on a label with a "for" attribute it triggers the event twice :
 - One on the label
 - One on the checkbox
 
The selector should be bound to the checkbox, not the label to be triggered only once
https://snipt.net/embed/ee33427ba4f6a5dba3e0d10d073175b7/
/raw/ee33427ba4f6a5dba3e0d10d073175b7/
ee33427ba4f6a5dba3e0d10d073175b7
text
Text only
6
2019-06-14T14:03:56
True
False
True
Oct 29, 2012 at 04:37 AM
/api/public/snipt/46589/
jquery-event-on-a-checkbox-called-twice
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a>That&#39;s because when you bind a click event on a label with a &quot;for&quot; attribute it triggers the event twice : </span><span id="L-2"><a name="L-2"></a> - One on the label </span><span id="L-3"><a name="L-3"></a> - One on the checkbox </span><span id="L-4"><a name="L-4"></a> </span><span id="L-5"><a name="L-5"></a>The selector should be bound to the checkbox, not the label to be triggered only once </span></pre></div> </td></tr></table>
event, jQuery