views:

14678

answers:

4

Hey,

Anyone know how to disable a link in jquery WITHOUT using return false;?

Specifically, what I'm trying to do is disable the link of an item, performing a click on it using jquery which triggers some stuff, then re-enabling that link so that if it's clicked again it works as default.

Thanks. Dave

UPDATE Here's the code. What it needs to do after the .expanded class has been applied is to re-enable the disabled link.

$('ul li').click(function(e) {
    e.preventDefault();
    $('ul').addClass('expanded');
    $('ul.expanded').fadeIn(300);
    //return false;
});
A: 

Just trigger stuff, set some flag, return false. If flag is set - do nothing.

Michał Chaniewski
Have tried that by setting a class, then calling that class later but return false carries through, and prevents the link from being called.
davebowker
+13  A: 
$('#myLink').click(function(e) {
    e.preventDefault();
    //do other stuff when a click happens
});

That will prevent the default behaviour of a hyperlink, which is to visit the specified href.

From the jQuery tutorial:

For click and most other events, you can prevent the default behaviour - here, following the link to jquery.com - by calling event.preventDefault() in the event handler

If you want to preventDefault() only if a certain condition is fulfilled (something is hidden for instance), you could test the visibility of your ul with the class expanded. If it is visible (i.e. not hidden) the link should fire as normal, as the if statement will not be entered, and thus the default behaviour will not be prevented:

$('ul li').click(function(e) {
    if($('ul.expanded').is(':hidden')) {
        e.preventDefault();
        $('ul').addClass('expanded');
        $('ul.expanded').fadeIn(300);
    } 
});
karim79
Yes, but can you re-enable the hyperlink after it's been disabled? How do you do that? Is there a "e.enableDefault();" command, or similar?
davebowker
@davebowker - Please see my edit
karim79
@karim- I know you didn't meant to post that chunk of info. twice
TStamper
@karim79, That works! I did try the prevent default method, but it was the 'if' rule that nailed it. Thanks.
davebowker
@TStamper - no I did not, thanks for that :)
karim79
This is "the way" :)
Jeff Davis
+1  A: 

Try this:

$("a").removeAttr('href');

EDIT-

From your updated code:

 var location= $('#link1').attr("href");
 $("#link1").removeAttr('href');
 $('ul').addClass('expanded');
 $('ul.expanded').fadeIn(300);
 $("#link1").attr("href", location);
TStamper
Not what I need. This will remove the link, but not allow me to re-enable it later.
davebowker
updated with what you wanted
TStamper
Why can't you store it before you removeAttr and then put it back when you are ready?
Topher Fangio
Danget, you fixed your answer as I was typing my previous comment =P
Topher Fangio
@Topher- that is what I updated to do
TStamper
@TStamper - So what we've learned from this is that StackOverflow needs to have chat sessions instead of comments :-D
Topher Fangio
@Topher-lol..I guess.. are you saying like Google Wave?
TStamper
@TStamper, @Topher, Trying this now. Thanks.
davebowker
A: 

Hey I tried this answer " $("a").removeAttr('href'); " he as i had the same issue to resolve this seems to work fine for what I needed to accomplish. Thanks for the help.

jake.cook1