Adrian
Adrian

Reputation: 2656

Checkbox: disable/enable href in jQuery

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

Answers (3)

J&#248;rgen
J&#248;rgen

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');
  }
});

http://jsfiddle.net/hQJXW/3/

Upvotes: 1

Brian Ray
Brian Ray

Reputation: 1492

$('#check').mousedown(function(){
    if(!$(this).is(':checked')) {
        $('.link').click(function(e){
                e.preventDefault();
        });
    }
});

Upvotes: 1

Interrobang
Interrobang

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

Related Questions