Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get which element was clicked?

I have a simple div in my html as follows:

<div id="myDiv">
....

</div>

Also I have set the onlick event on the window.click as follows:

window.onclick = function()
{
    // do something
 }  

So if I click, anywhere in the div, how can I find that this click was made inside "myDiv"

Note : I cannot add the click event on my div, it is generated randomly from jqgrid

like image 492
Sashi Kant Avatar asked Apr 14 '15 12:04

Sashi Kant


People also ask

How do I know which element was clicked?

To check if an element was clicked, add a click event listener to the element, e.g. button. addEventListener('click', function handleClick() {}) . The click event is dispatched every time the element is clicked. Here is the HTML for the examples in this article.

How do you get the ID of a clicked element in react?

To get the id of the element on click in React: Set the onClick prop on the element to a function. Access the id of the element on the currentTarget property of the event . For example, event.currentTarget.id returns the element's id .

How do I know which button is clicked in HTML?

The Html <button onclick=" "> is an event attribute, which executes a script when the button is clicked. This attribute is supported by all browsers. It is also used to call a function when the button is clicked.

What is the JavaScript event used to get the element clicked?

The onclick event occurs when the user clicks on an element.


1 Answers

$(document).on("click","#myDiv", function (event) {
     alert(event.target.id);
});
like image 83
renakre Avatar answered Sep 22 '22 19:09

renakre