Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
153
rated 0 times [  160] [ 7]  / answers: 1 / hits: 29273  / 10 Years ago, tue, july 15, 2014, 12:00:00

I have this label:



<label><input type=checkbox> I agree to the <a href=terms>terms</a> and would like to continue</label>


However, the link inside the label opens a foundation 5 reveal modal. This works fine, but clicking on the link also checks the checkbox.



How can I prevent the checkbox from being checked when the link is clicked?


More From » jquery

 Answers
24

You should just need to bind an event to the link that calls event.stopPropagation() and event.preventDefault()



JQuery for all links in labels:



$(document).on(tap click, 'label a', function( event, data ){
event.stopPropagation();
event.preventDefault();
window.open($(this).attr('href'), $(this).attr('target'));
return false;
});


Pure javascript (you need to set an id on the link you want to follow)



var termLink = document.getElementById('termLink');
var termClickHandler = function(event) {
event.stopPropagation();
event.preventDefault();
window.open(termLink.href, termLink.target);
return false;
};
termLink.addEventListener('click', termClickHandler);
termLink.addEventListener('touchstart', termClickHandler);


These expect the link target to be set to _self or _blank to open in the same window or a new window respectively.


[#70206] Friday, July 11, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
scarlett

Total Points: 491
Total Questions: 94
Total Answers: 100

Location: New Caledonia
Member since Thu, Mar 23, 2023
1 Year ago
;