JS get the clicked element with event.target

2024/2/27 7:17:36

I am trying to use JavaScript to get a clicked element's information.

Here is the jsFiddle.

And below is my code.

let div = document.querySelector('div')
div.addEventListener('click', function(event) {
// how to get the clicked p tag here?
// this?
// event.target.currentTarget?
<p>text 1</p>
<p>text 2</p>

How can I use this to get the element's information?

Answer: event.target Thanks Juan Mendes


e.target should give you the exact element that was clicked to generate the event.

e.currentTarget will give you the element that the event is at when the function was called (after bubbling up from the e.target)

div.addEventListener('click', (e) => {
console.log(e.target) // the p that was clicked
console.log(e.currentTarget) // this div

Related Q&A

How do I programmatically download a file using the browsers native download widget in Angular?

So, when I request my webservice for getting download a zip file, it downloads the file content secretely and all of a sudden, the file appears in the download task bar but already downloaded full (100…

How to disable smooth scrolling in IE11

IEs smooth scrolling is causing my app behave strange (scroll events are fired with a small delay).Is there a way to completely disable smooth scrolling in IE11 using CSS or Javascript?

Removing rows when reading data D3

Say I have a sample file sample.csv:row,col,value 1,1,2 1,2,3 1,3,NAWhen reading data in d3 you do something like:d3.csv("sample.csv", function(data) {data.forEach(function(d) {d.value = +d.v…

Angularjs: ng-options: How to order options with group and non-group

I have my group by ng-options looks like this<select ng-model="selected" ng-options="d.title group by d.group for d in data"></select>Here is my data$scope.data = [{grou…

Making Live Clock javascript

does anyone know how to make live javascript time running.. i have this php code$expiredate = date(d m Y G:i:s, $rdate1);$f_ex_date = explode(" ", $expiredate);$f_ex_time = explode(":&qu…

Prevent zombie.js from loading only external resources

I am using zombie.js to load a page from a local express server during a test. Unfortunately, there is a script element which calls out to Google Analytics. I would like to block this external script (…

Get value of current event handler using jQuery

I can set the onclick handler using jQuery by calling $(#id).click(function(){console.log(click!); });Also using jQuery, how can I get a reference to the function which is currently handling the click(…

Javascript Fullcalendar - copying events

Im using Fullcalendar (http://arshaw.com/fullcalendar) in my project. It gets events via json source. I want to give the user option to copy one event on the calendar to other day - and Id like to use …

Current time in setInterval?

setInterval(function() {var current = ? getCurrentInterval ? ;alert(current); }, 2000);Is possible to check?

Ajax POST method converts my + value in a string to why?

I cant figure out why in ajax post "+" sign converts to " ".please explain ?