tags:

views:

292

answers:

3

I have a page with some links inside a div and some outside that div.

  <div id="navigation">
     <ul>
       <li>
         <a href="/Home/Index">Home</a></li>
      <li>
         <a href="/Home/contactus">Contact Us</a></li>
    </ul>
   </div>
<a href="/User/SignIn">Sign In</a>......

I need to disable the click event for the all the links except inside the navigation div.

How to do it in jquery using similar to:

//disable Click event for links
    $("a:not([id])").live('click', function(e) {
        e.preventDefault;
        return false;
    });
+2  A: 

This doesn't, in general, sound like a good idea, but here's how I'd do it:

$('a[href]').live ('click', function (e)
{
    if (!$(this).parents('#navigation').length))
        return false; // same as e.preventDefault() & e.stopPropogation()
});
K Prime
Oh dear.. upon poring over the jQuery Live Events (http://docs.jquery.com/Events/live) page, I realised that *e.stopPropogation()* would not work as expected (thanks to the implementation via delegation) - so *return false* is exactly the same as *e.preventDefault()*
K Prime
A: 

Inspired by K Prime's answer:

$('a')
 .filter(function(){return $(this).parents('#navigation').length == 0})
 .live('click', function(e) {
   return false;
 });
orip
According to the jQuery docs (http://docs.jquery.com/Events/live), they only use the selector itself for live binding, so the initial filter won't work.
K Prime
+1  A: 

After a try, the below implementation worked:

 //disable Click event for links except navigation
    $("a:not(#navigation a)").live('click', function(e) {
        e.preventDefault;
        return false;
    });

Any flaws in this

Prasad
Initially I was under the impression that _:not()_ only accepted simple selectors, but further checking reveals that your implementation is valid and would suffice
K Prime