views:

24658

answers:

5

Is there any way to get the ID of the element that fires an event?

I'm thinking something like:

<html>
  <head>
    <script type="text/javascript" src="starterkit/jquery.js"></script>
    <script type="text/javascript">
      $(document).ready(function(){
        $("a").click(
          function(){
            var test = caller.id;
            alert(test.val());
        }
      );
    });

  </script>
</head>
  <body>
    <form class="item" id="aaa"><input class="title"></input></form>
    <form class="item" id="bbb"><input class="title"></input></form>
  </body>
</html>

Except ofcourse that the var test should contain the id "aaa", if the event is fired from the first form, and bbb, if the event is fired from the second form.

Can anyone help with this?

/Jonas

+13  A: 

You can use (this) to reference the object the fired the function.

'this' is a DOM element when you are inside of a callback function (in the context of jQuery), for example, being called by the click, each, bind, etc. methods.

Espo
'this' is true. :]
matt lohkamp
+24  A: 

In jQuery event.target always refers to the element that triggered the event, where 'event' is the parameter passes to the function. http://docs.jquery.com/Events_(Guide)

$(document).ready(function() {
    $("a").click(function(event) {
        alert(event.target.id);
    });
});

Note also that 'this' will also work, but that it is not a jQuery object, so if you wish to use a jQuery function on it then you must refer to it as '$(this)', e.g.:

$(document).ready(function() {
    $("a").click(function(event) {
        // this.append wouldn't work
        $(this).append(" Clicked");
    });
});
samjudson
Wow! Thanks. Didn't realize that jQuery abstracted that so well. You rock! I would think it would still have the difference between (this) and (event.target) -- being object you bound the event to vs. the object that received the event.
Hafthor
Unfortunately, event.target doesn't permit access to its custom attributes. To do that, one must use $(this).attr("organization:thingy");.
Zian Choy
thanks for the diff between this and $(this)
Michel
+3  A: 

Very helpful code! I was able to use an alert to show the id of what was clicked! But Im trying to show/hide elements based on what is clicked.

So for example, if you click link 1 (id="1") - all div's with id="1" will show, but div's with id="2" will be hidden. I can get everything to show, but not just id="1".

Here is my code, what am I doing wrong?

$(event.target.id)show(); $(event.target.id:not).hide();

Here is the full code:

 //grab values from attributes assigned in xsl
 var qtID = $(this).attr("id");

 // make id to show comment (#23453-3djis-im37vk9-fkifhf7)
 var ccID = '#' + qtID;


 //show only comments of matching ccID that were clicked
 $("#item-comments-listing").show();
 $(event.target.id)show();
 $(event.target.id:not).hide();

Any ideas?

jrutter
thanks a lot!! i was looking for $(this).attr("id");
Deepak Yadav
A: 

Hellow every one how to deal when the same id reapeats as i have problem in the code.

<script type="text/javascript">
  $(document).ready(function(){
    $("input").click(
      function(){
        alert(this.id);
        $('#comments'+this.id).show();
    }
  );
});

.comments{ width:500px; height:350px; background:#06F; display:none; }

box 1 box 2 box 3

liaqat
A: 

I'm a tad late. lol. For reference, try this! It works!

jQuery("classNameofDiv").click(function() {
            var contentPanelId = jQuery(this).attr("id");
            alert(classNameofDiv);

        });
Gemma