How to get the id of the element clicked using jQuery

How to get the id of the element clicked using jQuery:




How I can get the id of the current element I am looking at. I use a jquery selector with only classes to get the elements. I am trying to get the attribute value of a clicked item with jQuery, any attribute which is added in that HTML tag (id, class, name, title, src, etc.).

Now we have this code.

<div class="main_div">
    <div id="inner_div">  
        <span id="d1" class="get_clicked">click to get id</span>
    </div>
</div>





And I want to get the id of the clicked element? The span which is present inside the inner_div will be having different ids because I will be loading the span from the model(MVC) using jquery ajax. So there will be 'n' number of span. The entire span will have unique id. I want to get the id of the span which I click.

$(document).on('click', 'span', function () {
    alert(this.id);
});


OR Use this



$('span').on('click', function () {
    alert(this.id);
});
this refers to current span element clicked

this.id will give the id of the current span clicked




JQuery related Posts:
·                     Check Uncheck all asp.net CheckBox in asp.net using jQuery
·                     Example of jQuery Validate on Radiobuttonlist in Asp.Net using C#
·                     Example jQuery Validate on CheckBoxList using C#
·                     Check Uncheck all html CheckBox controls using jQuery:
·                     fill data into Dropdown list by using Jquery
·                     Validate ASP.Net RadioButtonList using JavaScript Example
·                     Example of jQuery Validate on Radiobuttonlist in Asp.Net using C#
·                     Example jQuery Validate on CheckBoxList using C#
·                     Check Uncheck all asp.net CheckBox in asp.net using jQuery
·                     Check Uncheck all html CheckBox controls using jQuery:
·                     Asp.net CheckBoxList using jQuery.
·                     Cropping image using jQuery in asp.net
·                     Displaying the textbox value in javascript Messagebox
·                     Get selected radio button values using JQuery.
·                     How do you do html text encodes using JavaScript
·                     Limit Number of Characters in a TextArea using jQuery
·                     jquery disable or Enable submit button after validation
·                     Enable Disable Submit Button using jQuery
·                     JQuery UI Datepicker (Calendar) with asp.net textbox
·                     Get current datetime in jquery and javaScript.


Comments