Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery - disable click

I just want to disable the ability for a user to click on an element for some condition. Here is some of the code I am working with:

     $('#navigation a').bind('click',function(e){      var $this   = $(this);     var prev    = current;      current = $this.parent().index() + 1; // store the position in current      if (current == 1){     $("#navigation a:eq(1)").unbind("click"); // i want to disable the ability to click this element if current is 1     }     if (current >= 2){     $("#navigation a:eq(1)").bind("click"); // this is wrong, but I want to rebind the click if current is greater than 1.       } 

}

like image 993
scifirocket Avatar asked Jan 04 '11 01:01

scifirocket


People also ask

How do I turn off the click function?

Use off() method after click event is triggered to disable element for the further click. $('#clickElement'). off('click'); The following code snippet is a real-time example for disabling click event using jQuery.

Is button disabled jQuery?

Checking if the button is disabled or enabled with jQuery removeAttr('disabled'); }; const checkButton = (e) => { const isDisabled = $('#my-button'). prop('disabled'); if( isDisabled ){ alert("Is disabled!"); } else{ alert("Is enabled"); } }; $(document).

How do I turn off all click events?

Dynamically disable all clicks on pagelet freezeClic = false; // just modify that variable to disable all clics events document. addEventListener("click", e => { if (freezeClic) { e. stopPropagation(); e. preventDefault(); } }, true);

How do I enable and disable a div?

If you want to disable all the div's controls, you can try adding a transparent div on the div to disable, you gonna make it unclickable, also use fadeTo to create a disable appearance.


2 Answers

If you're using jQuery versions 1.4.3+:

$('selector').click(false); 

If not:

$('selector').click(function(){return false;}); 
like image 75
zzzzBov Avatar answered Oct 15 '22 23:10

zzzzBov


assuming your using click events, just unbind that one.

example

if (current = 1){      $('li:eq(2)').unbind("click"); } 

EDIT: Are you currently binding a click event to your list somewhere? Based on your comment above, I'm wondering if this is really what you're doing? How are you enabling the click? Is it just an anchor(<a> tag) ? A little more explicit information will help us answer your question.

UPDATE:

Did some playing around with the :eq() operator. http://jsfiddle.net/ehudokai/VRGfS/5/

As I should have expected it is a 0 based index operator. So if you want to turn of the second element in your selection, where your selection is

$("#navigation a") 

you would simply add :eq(1) (the second index) and then .unbind("click") So:

if(current == 1){     $("#navigation a:eq(1)").unbind("click"); } 

Ought to do the trick.

Hope this helps!

like image 38
ehudokai Avatar answered Oct 15 '22 22:10

ehudokai