Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to prevent checkbox check when clicking on link inside label

I have a link inside a label. The problem is, when user clicks 'back' after having read the terms, the checkbox is unchecked, because when they clicked on the link they also unchecked the box at the same time, since the link is inside a label.

<input type="checkbox" id="terms" name="terms" checked="checked" /> 
<label for="terms">I agree to be bound by the <a href="/terms">Terms</a></label>

How can I prevent the checkbox from being checked when link is clicked? Tried doing event.preventDefault() on label click, but that doesn't prevent checkbox from being checked/unchecked.

I could just take out the link from inside a label (which means more CSS styling). But now I'm curious whether the above is possible.

like image 271
montrealist Avatar asked Jul 08 '09 13:07

montrealist


1 Answers

You can cancel the click event by routing it through an onclick event.

The "return false;" part will prevent the click event from moving up to the label.

<input type="checkbox" id="terms" name="terms" checked="checked" /> 
<label for="terms">I agree to be bound by the <a href="#" onclick="window.open('/terms','_blank');return false;">Terms</a></label>
like image 167
Jason Lawton Avatar answered Sep 21 '22 14:09

Jason Lawton