I have a div
tag in my form without id
property. I need to set an on-click
event on this div
tag.
My HTML code:
<div class="drill_cursor" > .... </div>
I don't want to add an id
property to my div
tag.
How can I add an on-click
event on this tag using JavaScript?
To add a click event listener on div tag using JavaScript, we can call the addEventListener method on the selected div. to add a div with a class.
The click() function is built in JavaScript, not jQuery. Source. If you're talking about attaching event handlers, the click() in $('div'). click(...) is most definitely a jQuery function.
We simply add the onlcick event and add a location to it. Then, additionally and optionally, we add a cursor: pointer to indicate to the user the div is clickable. This will make the whole div clickable.
document.getElementsByClassName('drill_cursor')[0] .addEventListener('click', function (event) { // do something });
$(".drill_cursor").click(function(){ //do something });
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