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>
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
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:
Comments
Post a Comment