Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8655769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:07:13+00:00 2026-06-12T15:07:13+00:00

HTML <div class=mybox id=element1> <div class=mybox id=element2> <div class=mybox id=element3> </div> <div class=mybox id=element4>

  • 0

HTML

<div class="mybox" id="element1">
    <div class="mybox" id="element2">
        <div class="mybox" id="element3">
        </div>
        <div class="mybox" id="element4">
        </div>
    </div>
</div>

JS

$(".mybox").live("mouseup", function(event) {
   alert($(this).attr("id"));
});

Okay, so my question is how to get only the active div (the one that I have clicked on, not all the parents). When I click on element2 I get element2 & then “element1” active, when I click on element3 I get element2 & then element1 active again, etc.

Any suggestions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T15:07:14+00:00Added an answer on June 12, 2026 at 3:07 pm

    The reason you see all those alerts is due to event bubbling. Each time you click on an element, the event is propagated up the chain. Every element which has a click event attached along the way will be executed.

    To ensure you only execute the code when the current element has been clicked on and to ignore bubbled events change your event to check if that the current element is the one you clicked on using event.target and only then execute your intended code:

    $(".mybox").live("mouseup", function(event) {
        if(event.target === this){
           alert($(this).attr("id"));
        }
    });​
    

    DEMO – using live

    If you are using jQuery 1.7 or later live() was deprecated and on() is now the preferred way of attaching and removing events.

    To use on() instead you can do this:

    (document).on("mouseup", ".mybox", function(event) {
        if (event.target === this) {
            alert($(this).attr("id"));
        }
    });​
    

    DEMO – using on()

    When using on() you specify the closest static element which in the example above is the document. However, if you have a closer static element you should be using that instead of document.

    I have linked all the required documentation in the answer but to summarise again.

    Resources

    • live() – has all the details on why it has been deprecated
    • on() – preferred since 1.7
    • delegate() – which can be used instead of live since 1.4.2 or later
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following HTML and CSS in a jsfiddle : <div class=MyBox> <div
i have my html something like this... <div class = content> <div class =
<div class = a> <div class = a b> text </div> </div> <script> alert($(.a).html());
Is this html <div class='title centered'>SCE</div> <div class='description'> Magna tristique pulvinar porta montes, scelerisque
I have this HTML code: <div class='com_box'> <div class='com_box_c'> <div class='com_box_info'> <a id='quote'>quote</a> </div>
Suppose I have the following html: This a test of <code>some code</code>. <div class='highlight'>
Consider the following code: $(document).ready(function() { $(body).append(<div class='outer'><span class='inner'>Click me</span></div>); $(html).click(function(event) { var targetClass
This is my html : <div id=my_box_one class=head-div> <div> <div class=some_box>a</div> <div class=some_box>b</div> </div>
I know that this I can do with function load $('#result').load('what.html #div-id'); But is
I'm developing a html with this structure div id=menuContainer> <div class = menu ui-accordion-header

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.