Getting the ID of the element that fired an event


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

I'm thinking something like:

$(document).ready(function() {
  $("a").click(function() {
    var test =;
<script type="text/javascript" src="starterkit/jquery.js"></script>

<form class="item" id="aaa">
  <input class="title"></input>
<form class="item" id="bbb">
  <input class="title"></input>

Except of course 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.

6/26/2020 8:38:41 PM

Accepted Answer

In jQuery always refers to the element that triggered the event, where event is the parameter passed to the function.

$(document).ready(function() {
    $("a").click(function(event) {

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");
5/15/2019 5:24:26 AM

For reference, try this! It works!

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

Though it is mentioned in other posts, I wanted to spell this out:

$( is undefined

$([0].id gives the id attribute. also gives the id attribute. gives the id attribute.


$(this).id is undefined.

The differences, of course, is between jQuery objects and DOM objects. "id" is a DOM property so you have to be on the DOM element object to use it.

(It tripped me up, so it probably tripped up someone else)


For all events, not limited to just jQuery you can use

var target = || event.srcElement;
var id =

Where fails it falls back on event.srcElement for IE. To clarify the above code does not require jQuery but also works with jQuery.


You can use (this) to reference the object that 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.

Here is where you can learn more:


I generate a table dynamically out a database, receive the data in JSON and put it into a table. Every table row got a unique ID, which is needed for further actions, so, if the DOM is altered you need a different approach:

$("table").delegate("tr", "click", function() {
   var id=$(this).attr('id');