0

seems a little trivial but am having a hard time solving it, i have a jquery function to select the class of a tag on click but the problem is that it selects every other tag underneath the tag clicked as well in structural order where as i only want the very first one

for example if i have

<div class="1">
  <div class="2">
      <div class="3">
          <p class="4">Hello World</p>
      </div>
  </div>
</div>

and i clicked on the (p) tag that says hello world i would get an alert saying 4 then 3 then 2 then 1 but like i said i only want the first one witch in this case is 4

here is my jquery code

$("*").dblclick(function(){
    if(!$(this).hasClass("") && !$(this).hasClass("main")){
        alert($(this).attr('class'));
    }
});

i know the problem is happening because technically i am clicking all of the tags so it continus to loop trough but i still need a way to break it after the first class is selected or better yet is there a way to select only the topmost object

2
  • 1
    Use event.stopPropagation() or return false; from event handler Commented Oct 16, 2015 at 17:05
  • Just read about event bubbling and event capturing Commented Oct 16, 2015 at 17:08

4 Answers 4

1

Just add return to the function like so:

$("*").dblclick(function(){
if(!$(this).hasClass("") && !$(this).hasClass("main")){
    alert($(this).attr('class'));
    return false;
}
});
Sign up to request clarification or add additional context in comments.

6 Comments

Small Case return false;
dude thanks your a life saver such a tiny problem but its taken me the better part of a day
May I ask though why you are selecting all classes? Let me post a more efficient answer example, based on the given logic, unless htis was just for demo reasons.
It won't let me post an answer, but just select by the class main, the rest is not necessary
example: $("*").not(".main").dbclick(function(){alert($(this).class());});
|
1

I would pass in event to your click function, and after you've finished your logic, use event.stopPropagation(). This prevents the event from bubbling up to parent elements

$("*").dblclick(function(event){
    if(!$(this).hasClass("") && !$(this).hasClass("main")){
        alert($(this).attr('class'));
    }
    event.stopPropagation();
});

2 Comments

!$(this).hasClass("") not required
this is also a good solution. I will give this an answer point.
0

Run this example and look your console.

$("body > div").dblclick(function(){
   console.log($(this).attr('class'));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="1">
  <div class="2">
      <div class="3">
          <p class="4">Hello World</p>
      </div>
  </div>
</div>

Comments

0

You should read about event bubbling and event propagation.

Here is function which does what you want:

$("*").dblclick(function(e){
    if(!$(this).hasClass("") && !$(this).hasClass("main")){
        alert($(this).attr('class'));
        e.stopPropagation();
    }
});

And here is working example: https://jsfiddle.net/18sawk57/

Although it's not a good solution to attach event listening to all of the tags on the page. Much better solution is to add for example id or clickable class attribute for elements that should have event listening.

Here is another working example with better approach: https://jsfiddle.net/tr7aqask/

Here is another working example with bubbling disabled using jquery: https://jsfiddle.net/yc0481sm/

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.