-2

Help, solution to:

http://jsfiddle.net/guin90/mb5BJ/2/

$("#click").on("click", function(){
    if($(this).attr("thesame") == 1){
        // Performs div is clicked on it and not on other elements within it. 
        // It's not working!
    } else {      
        return false;
    }

    alert("Hellow");
    $(this).css("background", "red");

    // Only if you clicked the div # click and not on other elements within 
    // this div. But is not working!? For up when you click on the div 
    // or span element inside the div # click it also performs the function, 
    // it is only if clicked on div # click and not on other elements 
    // within it.

    // and performs other functions that I will put
});

HTML:

<div id="click" thesame="1">

    <!-- Clicks and does not perform function Click() -->
    <span> Title HI! </span>   

    <!-- Clicks and does not perform function Click() -->
    <div id="show_modal"> Show Modal Window </div>

</div>
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • 2
    Instead of asking a new question, you should have clarified your [original one](http://stackoverflow.com/q/17935233/218196). – Felix Kling Jul 30 '13 at 00:37
  • possible duplicate of [jQuery: click function exclude children.](http://stackoverflow.com/questions/2457246/jquery-click-function-exclude-children) – Felix Kling Jul 30 '13 at 00:45

1 Answers1

1

Use event.target to identify which element was clicked. Something like this should do the trick

$("#click").on("click", function(e){ 
    if (e.target.id != "click" ) return false;
    ....
};

Notice that you'll need to pass e as parameter of your event handler

DEMO

If you need to generalize the idea to any div in your page you should do

$("div").on("click", function(e){ 
    if (e.target.nodeName == "DIV") return false;
    ....
};
Claudio Redi
  • 67,454
  • 15
  • 130
  • 155
  • Friend, but what if I do not use # click only use $ ("div.") On ("click", ... As would be to validate that the user clicked in the same div and not other tags inside it? Would be: $ ("div".) Live ("click", function (e) {   if (e! = "click") return false; ? – user2632011 Jul 29 '13 at 23:47
  • @user2632011: you would use `event.target.nodeName == "DIV"` – Claudio Redi Jul 30 '13 at 00:00
  • @user2632011 you can also check for `event.target != this` – bfavaretto Jul 30 '13 at 00:25