1

I have a problem writing an if statement, due to my lack of programing skills.

there's the code :

   $("div.footerMenu li div.onScreen").click( 
  function(e) { e.stopPropagation(); e.preventDefault(); return false; } 
);

Inside this div I have simple <a></a> links. And the problem is, when i click on that link, nothing happens. I'm trying to make a function, that would not execute that function(e) if the target of .click would be an <a></a> tag. Is there a simple way to do that?

2
  • 2
    Just so you know, return false is the same as doing both e.stopPropagation and e.preventDefault. Commented Feb 1, 2011 at 12:27
  • @Jonathon Bolster: I agree. But does that also apply to bubbling when using delegation? Commented Feb 1, 2011 at 12:33

6 Answers 6

2

You problem is that because you're link is inside the div, your code is blocking the click event, hence why nothing happens. You could try this:

$("div.footerMenu li div.onScreen").click(function(e) { 
    if(e.target.tagName.toLowerCase() != 'a') {
        e.stopPropagation(); 
        e.preventDefault(); 
        return false; 
    }
});

I'm using .toLowerCase() because I'm not 100% the tagName will always be uppercase in all browsers. Just a sanity check.

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

Comments

1

I had a similar problem and here is the solution that works :)

$("div.footerMenu li div.onScreen").click(function(e) { 
    if( !( $(e.target).is('a') ) ) 
        e.stopPropagation(); e.preventDefault(); return false; 
    } 
);

2 Comments

i have up-voted your answer because testing for an "a" is much simpler done here using "is('a')"
Thank you, I hope the Szymon finds this one helpful
0

Yup, very easy

if (e.target.nodeName !== "A") {
    // your code here
}

Comments

0

You will need to check if the node has tagName == 'A'

if (this.tagName == 'A')

"this" should reference to the node being clicked, otherwise lookup e.target or e.targetSrc (don't know the jQuery equivalent)

Comments

0

When you click on an element, only events it will be running. But not events atached on parent elements. You need to change your selector to match all child elements.

$("div.footerMenu li div.onScreen > *") $("div.footerMenu li div.onScreen > a")

Comments

0

Rather than check if target was an a (to me, it's a bit messy), I'd prefer to separate it out and handle the links separately. By using e.stopPropagation only, you'll be able to click the link and have whatever action you want on it as well as preventing the event from bubbling up to the div.

$("div").click(function(e){
    alert('You clicked me, I am the div!');
});

$("div a").click(function(e){
    // Allows the link to be clicked but prevents
    // the event from bubbling up to the div
    e.stopPropagation();
}); 

Example: http://jsfiddle.net/jonathon/s2TxS/

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.