Saturday, April 13, 2024
 Popular · Latest · Hot · Upcoming
155
rated 0 times [  156] [ 1]  / answers: 1 / hits: 19887  / 15 Years ago, wed, july 8, 2009, 12:00:00

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.


More From » jquery

 Answers
30

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>

[#99167] Friday, July 3, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
elliem

Total Points: 415
Total Questions: 117
Total Answers: 94

Location: American Samoa
Member since Fri, Aug 26, 2022
2 Years ago
;