2

I want to perform an action when a user clicks on a div. However if he clicks on link within div, I want to send him directly to link. Now the href is being ignored.

<div id=myDiv>

   some text
   <a href="link1">my link</a>

   some text
   <a href="link2">my link</a>

   some text
   <a href="link3">my link</a>
<div>


$('.myDiv').live('click', function() {          
        console.log($(this));
        // if clicked on link, the go directly to the link
        ....
        // if clicked elsewhere on div, then perform another another action and return false
        return false;

    }); 

====== MY SOLUTION ====

I liked Rob's solution but the syntax was a bit vexing, so RTFM and came up with something similar that works for me.

$('.myDiv').live('click', function(event) {         
   var $target = $(event.target);
   if( $target.is("a") ) {
    return true;
    }
    else{
      // do something else
      return false;
    }
}); 
2
  • 1
    What happens if you return true;? Commented Nov 6, 2011 at 16:30
  • This works...performs the href after doing other stuff, but would prefer to go directly to link. Commented Nov 6, 2011 at 16:32

2 Answers 2

1

Check the event.target property. The code below does not execute return false when an anchor is clicked.

$('.myDiv').live('click', function(event) {
    ....
    // `return false` only if element != <a>
    if(!$.nodeName(event.target, "a")) return false;

}); 
Sign up to request clarification or add additional context in comments.

5 Comments

Doesn't he want if($.nodeName(e.target, "a")) return true; where the .... is?
@Hogan He wants to follow the link. return false prevents the link from being followed. I have modified his code so that return false will not occur when the current target is an anchor, which has the same effect as return true.
So the default is to return true? I did not see that in your code.
$.nodeName checks whether the given element (event.target = currently clicked element) is an <a> element ("a"). The ! before the statement negates the expression. So, if the current element is not an <a> element, the function returns false, effectively blocking click events. When a link is clicked, the function works. @Hogan Inside a click event handler return true is equivalent to just return.
sorry I meant my code was ambiguous fragment. Yours was just vexing.
1

Call stopPropagation() when the links are clicked:

$('.myDiv').live('click', function(e) {
    // Perform your action here
});

$('.myDiv a').live('click', function(e) {
    e.stopPropagation();
});

2 Comments

Isn't this the reverse of what he wants -- this stops a link from working, he wants the link to work.
@Hogan: Nope - stopPropagation() isn't preventDefault(). jsfiddle.net/8sfdQ (Hehe... the example doesn't work because of the cross-origin rule for iframes. Oops :P If you go to /show/ it does.)

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.