Except of course that the var demo should contain the id "aaa", if the event is fired from the first khung, & "bbb", if the event is fired from the second size.
Bạn đang xem: How to get id of an element with javascript/jquery – techie delight
$(document).ready(function() $("a").click(function(event) alert(sự kiện.target.id); ););chú ý also that this will also work, but that it is not a jQuery object, so if you wish lớn use a jQuery function on it then you must refer to lớn it as $(this), e.g.:
$(document).ready(function() $("a").click(function(event) // this.appkết thúc wouldn"t work $(this).append(" Clicked"); ););
Share
Improve this answer
Follow
edited May 15 "19 at 5:24
jQuery("classNameofDiv").click(function() var contentPanelId = jQuery(this).attr("id"); alert(contentPanelId););
Share
Improve this answer
Follow
edited Jul 2 "12 at 3:03
$(sự kiện.target).id is undefined
$(event.target)<0>.id gives the id attribute.
sự kiện.target.id also gives the id attribute.
this.id gives the id attribute.
and
$(this).id is undefined.
The differences, of course, is between jQuery objects and DOM objects. "id" is a DOM property so you have to be on the DOM element object khổng lồ use it.
Xem thêm: Top 12 Cung Hoàng Đạo Hợp Nhau Nhất Trong Tình Yêu, Những Cung Hoàng Đạo Hợp Nhau
(It tripped me up, so it probably tripped up someone else)
var target = event.target || sự kiện.srcElement;var id = target.idWhere event.target fails it falls baông xã on sự kiện.srcElement for IE.To clarify the above sầu code does not require jQuery but also works with jQuery.
"this" is a DOM element when you are inside of a callback function (in the context of jQuery), for example, being called by the clichồng, each, bind, etc. methods.
$("table").delegate("tr", "click", function() var id=$(this).attr("id"); alert("ID:"+id); );
Share
Improve sầu this answer
Follow
edited Feb 8 "16 at 15:13
Alexander Elgin
5,80644 gold badges3232 silver badges4747 bronze badges
answered Sep 12 "11 at 14:14
SMutSMut
29133 silver badges22 bronze badges
2
Add a phản hồi |
19
Element which fired event we have in event property
event.currentTargetWe get DOM node object on which was mix event handler.
Most nested node which started bubbling process we have in
event.targetEvent object is always first attribute of sự kiện handler, example:
document.querySelector("someSelector").addEventListener(function(event) console.log(sự kiện.target); console.log(event.currentTarget););More about sự kiện delegation You can read in http://maciejsikora.com/standard-events-vs-event-delegation/
var $el = $(event.target);This gets you the source of the clichồng, rather than the element that the click function was assigned too. Can be useful when the cliông chồng event is on a parent objectEG.a clichồng sự kiện on a table row, & you need the cell that was clicked
$("tr").click(function(event) var $td = $(event.target););
Share
Improve sầu this answer
Follow
edited Dec 15 "14 at 13:51
Cerbrus
59.2k1515 gold badges112112 silver badges129129 bronze badges
answered Mar 22 "12 at 15:41
MorvaelMorvael
2,98122 gold badges2929 silver badges5151 bronze badges
0
Add a phản hồi |
8
this works with most types of elements:
$("selector").on("click",function(e) log(e.currentTarget.id); );
Share
Improve this answer
Follow
edited Sep 13 "19 at 22:48
answered Oct 18 "18 at 19:28
CrisCris
2,0671818 silver badges1818 bronze badges
Add a phản hồi |
7
You can try khổng lồ use:
$("*").live("click", function() console.log(this.id); return false;);
Share
Improve sầu this answer
Follow
answered Nov 3 "12 at 17:40
DariusDarius
9911 silver badge33 bronze badges
Add a phản hồi |
6
In the case of delegated event handlers, where you might have sầu something lượt thích this:
Item 1 Item 2 Item 3 Item 4 Item 5 and your JS code like so:
$(document).ready(function() $("ul").on("cliông xã li", function(event) var $target = $(event.target), itemId = $target.data("id"); //do something with itemId ););You"ll more than likely find that itemId is undefined, as the nội dung of the LI is wrapped in a , which means the will probably be the sự kiện target. You can get around this with a small check, like so:
$(document).ready(function() $("ul").on("clichồng li", function(event) var $target = $(event.target).is("li") ? $(event.target) : $(sự kiện.target).closest("li"), itemId = $target.data("id"); //vày something with itemId ););Or, if you prefer to maximize readability (& also avoid unnecessary repetition of jQuery wrapping calls):
$(document).ready(function() $("ul").on("click li", function(event) var $target = $(event.target), itemId; $target = $target.is("li") ? $target : $target.closest("li"); itemId = $target.data("id"); //vày something with itemId ););When using sự kiện delegation, the .is() method is invaluable for verifying that your sự kiện target (ahy vọng other things) is actually what you need it khổng lồ be. Use .closest(selector) to lớn tìm kiếm up the DOM tree, và use .find(selector) (generally coupled with .first(), as in .find(selector).first()) to search down it. You don"t need to lớn use .first() when using .closest(), as it only returns the first matching ancestor element, while .find() returns all matching descendants.