Mdb Mdb - 1 year ago 81
Javascript Question

stop label from toggling the input checkbox

I have the following html code. When clicking on the label it toggles the checkbox.

<td><label for="startClientFromWebEnabled">Client Launch From Web:</label></td>
<td><input type="checkbox" id="startClientFromWebEnabled" name="startClientFromWebEnabled" data-bind="checked: StartClientFromWebEnabled, enable: IsEditable" onchange="startClientFromWebToggleRequiredAttribute()" /></td>

How can I prevent this? If I remove the
, It stops toggling but I need this because I have some logic that takes the id from the element that fires the event ...

Answer Source

The best solution would be to let label toggle the checkbox as that is intuitive and expected behaviour.

Second best solution is to make sure your checkbox is not nested inside label and label does not have for attribute. If you have some logic that depends on it, you can put data attributes on elements and use those in your logic.

<input type="checkbox" data-myid="1" />
<label data-myid="1">foo</label>

Last resort

You could prevent the default behaviour of the click event using jQuery:

$('label[for="startClientFromWebEnabled"]').click(function(e) { 

Please see this jsFiddle for an example.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download