Reputation: 2656
I want to enable/disable my <a href>
button via a checkbox (default = disabled). Is it possible?
$('#check').change(function() {
});
What is the next step? I think I need unbind function?
(I want to disable checkout button until user not clicking on Terms And Conditions checkbox). Thanks
Upvotes: 0
Views: 5672
Reputation: 9130
If you want to disable and enable a link, this should work:
$('.button').click(function(){ return false; }); // initially disabled.
$('#check').click(function() {
if(!$(this).is(':checked')){
$('.button').bind('click', function(){ return false; });
}else{
$('.button').unbind('click');
}
});
Upvotes: 1
Reputation: 1492
$('#check').mousedown(function(){
if(!$(this).is(':checked')) {
$('.link').click(function(e){
e.preventDefault();
});
}
});
Upvotes: 1
Reputation: 17434
Here is one solution. jsFiddle
HTML:
<a id="to-toggle" data-href="http://google.com/">My Link</a><br />
<input id="toggle" type="checkbox"> Toggle it
JS:
var link = $("#to-toggle");
$("#toggle").on("change", function() {
if(this.checked) {
link.attr("href", link.data("href"));
} else {
link.removeAttr("href");
}
});
Upvotes: 2