1

There's a link in the DIV

<div><a href="#">go</a></div>

Can I disable click event on the DIV, but still works on the link?

I use this for test, but seems not work:

$(this).click(function(event){
    var _self_link=$('a',this);
    if(event.target!=_self_link){dosomething}
});

Any solution with Jquery? Thanks.

1
  • make click event not on the div, but in the <a> Commented Dec 1, 2011 at 14:07

3 Answers 3

5

I think you should use stopPropagation.

Stops the bubbling of an event to parent elements, preventing any parent handlers from being notified of the event.

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

Comments

4

Try something like this:

$('div:has(a[href="your_link"])').click(function(e) {
    stopPropagation();
});

You can test it here http://jsfiddle.net/cnzvL/13/

4 Comments

no he wants the div to prevent default. Not the anchor. might want to change the answer before I downvote
@Laurence, I just tested this code on jsFiddle, and the value of $(this) is div, not anchor, so I think the answer is correct. The div is preventing the default action.
yes now it will work with the first representation you were selecting the anchor not the div. will remove my downvote.
This is my solution. Thanks guys. $('DIV').click(function(event){ var click_on_href=false; $("a",'DIV').click(function(event){ event.stopPropagation(); click_on_href=true; }); if(!click_on_href){dosomething} });
2
$('div').click(function(event){
    event.preventDefault();
    // and then do what you want
});

Edited: maybe I didn't understand the question correctly. Here is the second variant:

$('a').click(function(event){
    event.stopPropagation();
    // and then do what you want
});

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.