0

Using Bootstrap

  <ul class="nav nav-pills nav-stacked col-sm-2 hidden" id="menu">
                <li role="presentation" id="LiNewsFeed"><a href="javascript:GetNewsFeed();">News Feed</a></li>
                <li role="presentation" id="LiStatusUpdate"><a href="javascript:StatusUpdate();">Update Status</a></li>
                <li role="presentation" id="LiWriteWall"><a href="javascript:WriteOnWall();">Post On Wall</a></li> 
                <li role="presentation" id="LiNotifications"><a href="javascript:GetNotifications();">Notifications</a></li>
                <li role="presentation" id="LiLogOut"><a href="javascript:LogOut();">Logout</a></li> 
  </ul>

In Javascript, I am disabling some of the <li> like the following:

 $('#LiNewsFeed').addClass('disabled');

The Item in the List actually LOOKS disabled, when when I click on it, it actually calls the javascript function, therefore, what I need is to disable the <a href> not just the <li>

I tried adding this after $(document).ready:

$(".nav li.disabled a").click(function () {
                return false;
});

But it's not really doing anything.

What I need is to disable the <a href> directly after disabling <li> in my Js code, and not to depend on a click event...

Seems like there is no way to disable an <a href>, so I need a way around it

Any help would be appreciated.

5
  • use $(".nav li.disabled a").click(function(event){ event.preventDefault(); }); Commented Mar 11, 2015 at 10:51
  • @jQuery Thank you, but I want to disable the a object, and not depend on the click event, because it's getting mixed up within other events and not calling it right. Commented Mar 11, 2015 at 10:52
  • then you can unbind click event $(".nav li.disabled a").unbind('click') or in new version of jquery $(".nav li.disabled a").off('click') Commented Mar 11, 2015 at 10:54
  • check my answer may be it will help you Commented Mar 11, 2015 at 11:16
  • @HelpASisterOut Did you try my solution? Commented Mar 11, 2015 at 15:33

4 Answers 4

1

use below code. check working example JSFIDDLE

$(".nav li.disabled a").each(function(){
     $(this).attr('href','javascript:void(0);');
});
Sign up to request clarification or add additional context in comments.

2 Comments

If I call this from te browser console it works, but not from the code
did you check example?
1

As you are disabling LI in javascript (runtime), you should use .on to bind events on disabled links:

$(".nav").on('click', 'li.disabled a', function () {
    return false;
}); 

Comments

1

I would check on every link click if the parent has the disabled class.

$('.nav li a').click(function () {
    if($(this).parent('li').hasClass('disabled')) {
        return false;
    }
    return true;
});

EDIT, following more info from OP I would suggest the following:

$('.nav li a').each(function() {
    var $this = $(this);

    // store reference of 'href' attr in case link is re-enabled
    $this.data('href', $this.attr('href'));

    if ($this.parent('li').hasClass('disabled')) {
        // remove href attribute disabling click
        $this.removeAttr('href');
    } else {
        // restore href
        $this.attr('href', this.data('href'));
    }
});

This code should be run after you add/remove the disabled class on li elements.

EDIT 2 - Rather than you calling functions from the href of <a> links, you could do something like the following:

var events = {
    '#LiNewsFeed': 'GetNewsFeed',
    '#LiStatusUpdate': 'StatusUpdate'
    '#LiWriteWall': 'WriteOnWall',
    '#LiNotifications': 'GetNotifications',
    '#LiLogOut': 'LogOut'
};

for (var selector in events) {
    if (events.hasOwnProperty(selector)) {
        try {
            $(selector).click(function () {
                // assuming function is global
                if (typeof window[events[selector]] === 'function') {
                    // call function
                    window[events[selector]]();
                }
                // this is needed if the a element still has a href attr
                return false;
            });
        } catch (e) {
            console.log('Invalid Selector');
        }
    }
}

This way you can control the calling of the function, and check whether it should be called without altering the element, perhaps stick an

if (!$(this).parent('li').hasClass('disabled')) {
    ...
} 

around the function call.

2 Comments

But I am binding the action to the ahref inside the a href tag like the following <a href="javascript:GetNewsFeed();">. is there a way I can turn this into javascript:void() in runtime?
yeah, in that case you could do $(this).attr('href', 'javascript:void(0)'); or perhaps even remove it, $(this).removeAttr('href');
0

can you convert the a into span?

(code not tested)

$(".nav li.disabled a").replaceWith(function() { return "<span>" + this.innerHTML + "</span>"; });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.