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
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.
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 .
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.
The onclick event occurs when the user clicks on an element.
$(document).on("click","#myDiv", function (event) {
alert(event.target.id);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With