Coder Perfect

Obtaining the ID of the element that caused an event to occur.


Is it possible to obtain the ID of the element that triggers an event?

Something along these lines comes to mind:

Except that if the event is triggered from the first form, the var test should have the id “aaa,” and if the event is fired from the second form, the var test should include the id “bbb.”

Asked by Joda

Solution #1

The element that generated the event is always referred to as in jQuery, where event is the parameter supplied to the method.

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

This will also work, but it is not a jQuery object, so you must refer to it as $(this) if you want to use a jQuery function on it, for example:

$(document).ready(function() {
    $("a").click(function(event) {
        // this.append wouldn't work
        $(this).append(" Clicked");

Answered by samjudson

Solution #2

Try this as an example! It’s effective!

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

Answered by Gemma

Solution #3

Despite the fact that it has been discussed in prior postings, I wanted to emphasize the following:

$( is undefined

$([0]. The id attribute is returned by id.

The id attribute is also provided by

The id attribute is returned by


$(this).id is undefined.

The distinction is between jQuery objects and DOM objects, of course. Because “id” is a DOM property, it must be used on a DOM element object.

(It threw me off, therefore it threw someone else off, too.)

Answered by dsch

Solution #4

You can use jQuery for any event, not just jQuery.

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

For IE, if fails, event.srcElement is used instead. To be clear, the code above does not require jQuery, but it does work with it.

Answered by Ally

Solution #5

You can refer to the object that called the function using (this).

When you’re inside a callback function (in the context of jQuery), such as the click, each, bind, and other methods, ‘this’ is a DOM element.

Here’s where you can find out more information:

Answered by Espo

Post is based on