Amit T.
Amit T.

Reputation: 83

disable link if checkbox s unchecked

I want to disable link if user uncheck the checkbox.

tried something like:

$('#check1').click(function() {
  if(!$(this).is(':checked')){
    $('#tet1').bind('click', function(){ return false; });
  }else{
    $('#tet1').unbind('click');
  }
});

but it did not work. http://jsfiddle.net/LX7wH/

Why this is not working for me? where i'm wrong? Thank you!

Upvotes: 2

Views: 2134

Answers (3)

Matthew Riches
Matthew Riches

Reputation: 2286

I believe this is what you are after:

<a href="http://www.google.com">Google</a>
<input type="checkbox" id="checkBox" />

$("a").click(function(e) {
        if($("#checkBox").prop("checked") === true) {
            e.preventDefault();
            return false;
        } else {
            return true;
        };
    });

This will stop all links working however you can change the "a" selector to what ever you want.

http://jsfiddle.net/KDZDE/

Upvotes: 1

adeneo
adeneo

Reputation: 318242

You don't need to do anything when the checkbox is changed, just check if it's checked within an event handler for the anchor, and if it is checked, prevent the default action.

$('#tet1').on('click', function(e) {
    if ( $('#check1').is(':checked') ) e.preventDefault();
});

Upvotes: 9

Rajaprabhu Aravindasamy
Rajaprabhu Aravindasamy

Reputation: 67207

Try,

var xEnabled = true;

$('#check1').click(function() {
 xEnabled = $(this).is(':checked');
});

$('#tet1').click(function(){
   if(!xEnabled) { return false; }
})

DEMO

Upvotes: 2

Related Questions