Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add click event on div tag using JavaScript

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?

like image 344
Ashish Rathore Avatar asked Dec 25 '13 12:12

Ashish Rathore


People also ask

Can I add a click event to a div?

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.

Can you click on a div JavaScript?

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.

How do I make a div clickable?

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.


1 Answers

Pure JavaScript

document.getElementsByClassName('drill_cursor')[0]         .addEventListener('click', function (event) {             // do something         }); 

jQuery

$(".drill_cursor").click(function(){ //do something }); 
like image 92
HDT Avatar answered Oct 12 '22 05:10

HDT