Get last clicked item id

Posted by Peeter Kõbu on Stack Overflow See other posts from Stack Overflow or by Peeter Kõbu
Published on 2012-11-18T21:50:09Z Indexed on 2012/11/19 5:01 UTC
Read the original article Hit count: 174

Filed under:
|

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.

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about ASP.NET