jquery - disable click

JavascriptJquery

Javascript Problem Overview


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.  
	}

}

Javascript Solutions


Solution 1 - Javascript

If you're using jQuery versions 1.4.3+:

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

If not:

$('selector').click(function(){return false;});

Solution 2 - Javascript

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!

Solution 3 - Javascript

Raw Javascript can accomplish the same thing pretty quickly also:

document.getElementById("myElement").onclick = function() { return false; } 

Solution 4 - Javascript

You can use unbind method to remove handler that has been attached...

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

You can check what can unbind do ? Unbind manual

Solution 5 - Javascript

Use off() method after click event is triggered to disable element for the further click.

$('#clickElement').off('click');

Solution 6 - Javascript

/** eworkyou **//

$('#navigation a').bind('click',function(e){

    var $this   = $(this);
    var prev    = current;

    current = $this.parent().index() + 1; //

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

Solution 7 - Javascript

I used .prop('disabled', true) and it worked like a charm, no redefining, simple.

Had to time it out by 125ms as it interfered with the prop from Bootstrap Dropdown.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionscifirocketView Question on Stackoverflow
Solution 1 - JavascriptzzzzBovView Answer on Stackoverflow
Solution 2 - JavascriptehudokaiView Answer on Stackoverflow
Solution 3 - JavascriptBenView Answer on Stackoverflow
Solution 4 - JavascriptJoko WandiroView Answer on Stackoverflow
Solution 5 - JavascriptSajitha RathnayakeView Answer on Stackoverflow
Solution 6 - JavascriptE Work YouView Answer on Stackoverflow
Solution 7 - JavascriptJustinView Answer on Stackoverflow