---
title: "How to get the id of the element clicked using jQuery?"  
description: "How to get the id of the element clicked using jQuery?"  
author: "Anand Pandey"  
published: 2017-06-06  
updated: 2017-06-14  
canonical: https://www.mindstick.com/forum/34304/how-to-get-the-id-of-the-element-clicked-using-jquery  
category: "jquery"  
tags: ["jquery"]  
reading_time: 2 minutes  

---

# How to get the id of the element clicked using jQuery?

```
<div class="main_div">    <div id="inner_div">            <span id="d1" class="get_clicked">click to get id</span>    </div></div>
```

How to get the id of the clicked element?

The span which is [present inside](https://www.mindstick.com/forum/12935/android-not-able-to-access-the-button-in-a-listview-present-inside-a-fragment) the inner_div will be having different ids because I will be loading the span from the [model](https://yourviews.mindstick.com/view/81334/america-is-reopening-after-corona-lockdown-but-on-swedish-model)([MVC](https://www.mindstick.com/forum/155803/define-cache-profile-in-mvc)) using [jquery ajax](https://www.mindstick.com/forum/157890/how-does-the-jquery-ajax-method-work-what-are-some-common-options-and-parameters-used-in-it). So there will be 'n' number of span. All the span will have [unique id](https://www.mindstick.com/forum/33873/how-to-get-unique-id-of-alasset-for-identification-in-ios). I want to get the id of the span which I [click](https://www.mindstick.com/articles/12423/social-login-magento-2-one-click-to-register-social).

How to get the id of the span when clicked? How to do this using jQuery?

## Replies

### Reply by reiki portal

Since you are loading in the spans via ajax you will have to attach delegate handlers to the events to catch them as they bubble up.\

```
$(document).on('click','span',function(e){    console.log(e.target.id)})
```

you will want to attach the event to the closest static member you can to increase efficiency.\

```
$('#main_div').on('click','span',function(e){    console.log(e.target.id)})
```

is better than binding to the document for instance.

This question may help you understand

\

\

\

### Reply by Jonas Stuart

This code will help you surely....

```
$(document).on('click', 'span', function () {                alert(this.id);            });             or you can use .on             $('span').on('click',
function () {                alert(this.id);            }); 
```


---

Original Source: https://www.mindstick.com/forum/34304/how-to-get-the-id-of-the-element-clicked-using-jquery

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
