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 8849467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:39:37+00:00 2026-06-14T12:39:37+00:00

I have function like this: $(document).ready(function () { $(*).click(function () { alert($(this).attr(‘id’).toString()); }); });

  • 0

I have function like this:

$(document).ready(function () {
          $("*").click(function () {             
              alert($(this).attr('id').toString());                
        });
    });

And on Page i have something like this:

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<script language="javascript" src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<div id="div1">Some stuff
    <div id="div2">Some other stuff
    <asp:Button ID="Button1" runat="server" Text="Button" />        
        <div id="div3">More stuff
        <asp:Button ID="Button2" runat="server" Text="Button" />        
        </div>
    </div>        
</div>   

If i click something it works fine, but it alerts me three times(or more).
For example:
I click button2. Alertbox appears with button2.id, then with div3.id, div2.id etc. It shows me all id’s under that button.
If i try to store this id to variable like this:

     var storedId = $(this).attr('id').toString();

it stores the last one.That means i get the id of form1.

How can i get the first id? The id of clicked button or clicked label or whatever i have on my page.

  • 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-14T12:39:38+00:00Added an answer on June 14, 2026 at 12:39 pm

    You need to stop event propagation up the DOM tree so parent items don’t see the same event. In jQuery, you can do that with e.stopPropagation():

    $("*").click(function(e) {
        alert($(this).attr('id').toString());                
        e.stopPropagation();
    });
    

    FYI, this is a really inefficient way to do this. You would do it better like this with a single event handler that takes advantage of event bubbling and catches the bubbled click events at the document and examines them in just this one place:

    $(document).click(function(e) {
        alert(e.target.id);                
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some jQuery code something like this: $(document).ready(function() { $(img.off).click(function() { alert('on'); $(this).attr('class',
I have the following <script> $(document).ready(function() { $(.mapLink).click(function(){ pos = $(this).attr(id); alert(pos); }); });
I have a code like this $(document).ready(function(){ $(.add).bind ('click', function () { $('.main').append('<div class=add>Add
I have a JS code like this <script type=text/javascript> $(document).ready(function(){ $('.main').on('click', '.block', function (){
So I have something like this situation: $(document).on('click', 'a[data-link]', function () { var $this
I have my jQuery code like this $(document).ready(function () { var lastsel; $('#jqgProducts').jqGrid({ //url
I have got code like this var challegneListener; $(document).ready(function(){ var challegneListener = setInterval(challengeListenerBot(),5000); });
I have some code like this: var content = document.getElementById('myDivId'); function MyFunction() { alert(content.style.height);
I have this: jQuery(document).ready(function(){ jQuery('a.color2030').click( function() { jQuery('tbody').hide(); jQuery('tbody.color2030').show(); }); jQuery('a.color2031').click( function() { jQuery('tbody').hide();
$(document).ready(function () { var user = 1; $(a.like).on(click, function () { var article =

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.