Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversHalloween DealsAmazon USHalloween week deals: useful picks before month-endAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan Now×
FREEDOM251

Freedom251 › Blog › event handling

How to Get the ID of the Clicked Element in jQuery

By Freedom251 Team · · 2 min read

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

In a regular jQuery click handler, use this.id to get the ID of the element whose handler is running. If you need the element that actually originated the click—perhaps a nested icon inside a button—use event.target.id instead.

Get the ID in a directly attached click handler

Attach the handler with .on(), then read the element’s native id property. In a regular function handler, this refers to the element running the handler. You can also use event.currentTarget for that element, or wrap this in jQuery to call jQuery methods.

As an Amazon Associate I earn from qualifying purchases.

$('#myButton').on('click', function (event) {
  console.log(this.id);                // Native DOM property
  console.log(event.currentTarget.id); // Element handling the event
  console.log($(this).attr('id'));     // jQuery method
});

For a direct handler, this and event.currentTarget ordinarily identify the same element. The jQuery API documents event.currentTarget and the jQuery() function used to wrap a DOM element.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose the right event property

“Clicked element” can mean either the element that received the original click or the element whose handler is running. These are not always the same: clicking a child element can initiate an event that bubbles to a parent handler.

Use What it identifies Example
event.target The element that initiated the event; it may be a nested child. event.target.id
event.currentTarget The element whose handler is currently running. event.currentTarget.id
this In a regular jQuery handler, the DOM element whose handler is running. this.id
$(this).attr('id') The same handler element, accessed through a jQuery method. $(this).attr('id')

The API defines event.target as the DOM element that initiated the event. Choose it when you want the ID of the original source, not necessarily the parent element handling the click.

Get the matched element in a delegated handler

Delegation lets an ancestor handle clicks that bubble up from descendants matching a selector. In this example, the handler is attached to #list but runs for matching buttons. Within the handler, this and event.currentTarget refer to the matched button; event.target may refer to a nested child inside it.

$('#list').on('click', 'button', function (event) {
  console.log(this.id);                // Matching button's ID
  console.log(event.currentTarget.id); // Matching button
  console.log(event.target.id);        // Element that originated the click
});

The jQuery guide to event delegation explains the pattern. For practical use, choose a nearby stable ancestor as the delegation point: the .on() documentation notes that delegated handlers placed high in the document tree may require extra selector comparisons.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Handle arrow functions correctly

Arrow functions do not have their own this, so do not rely on this.id in an arrow-function handler. Use the event argument’s currentTarget instead:

$('#myButton').on('click', (event) => {
  console.log(event.currentTarget.id);
});

This follows JavaScript’s lexical this behavior; event.currentTarget still identifies the element whose handler is running.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Use .on() for event binding

The .on() method was added in jQuery 1.7, and the jQuery Learning Center describes it as the preferred event-binding practice for jQuery 1.7 and later. The API examples currently load jQuery 4.0.0. See the official event-handling guide for the binding guidance.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

event handlingJavaScriptjQueryWeb development

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.