48

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?

6 Answers 6

63

Pure JavaScript

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

jQuery

$(".drill_cursor").click(function(){
//do something
});
Sign up to request clarification or add additional context in comments.

Comments

40

Try this:

 var div = document.getElementsByClassName('drill_cursor')[0];

 div.addEventListener('click', function (event) {
     alert('Hi!');
 });

Comments

20

Just add the onclick-attribute:

<div class="drill_cursor" onclick='alert("youClickedMe!");'>
....
</div>

It's javascript, but it's automatically bound using an html-attribute instead of manually binding it within <script> tags - maybe it does what you want.

While it might be good enough for very small projects or test pages, you should definitly consider using addEventListener (as pointed out by other answers), if you expect the code to grow and stay maintainable.

3 Comments

It is JavaScript! This is inline JavaScript code, and it really is widely adopted bad practice. It is hard to debug and maintain code, that is placed in various places over your html document. I recommend to use event handlers.
I upvote this one, because I needed a solution that could be used in a partial. The others required that I know the specific DIV I want to call the action on when clicked. My page has dynamic divs generated and I needed a way to call JS on an individual DIV when clicked. And I was not sure if you could call onClick on a DIV. Thank you!
The solution from @maja allowed me to check that it works. My onClick event is more like this: onClick:"toggleReadStatus(" + cue.id.to_s + ")" (RUBY code). So, speaking to @Andrew Surzhynskyi 's point, the JS code can be in a particular section for easy maintenance (and reduced repetition), but available to each dynamically generated DIV.
9

Recommend you to use Id, as Id is associated to only one element while class name may link to more than one element causing confusion to add event to element.

try if you really want to use class:

 document.getElementsByClassName('drill_cursor')[0].onclick = function(){alert('1');};

or you may assign function in html itself:

<div class="drill_cursor" onclick='alert("1");'>
</div>

Comments

4

the document class selector:

document.getElementsByClassName('drill_cursor')[0].addEventListener('click',function(){},false)

also the document query selector https://developer.mozilla.org/en-US/docs/Web/API/document.querySelector

document.querySelector(".drill_cursor").addEventListener('click',function(){},false)

Comments

1

Separate function to make adding event handlers much easier.

function addListener(event, obj, fn) {
    if (obj.addEventListener) {
        obj.addEventListener(event, fn, false);   // modern browsers
    } else {
        obj.attachEvent("on"+event, fn);          // older versions of IE
    }
}

element = document.getElementsByClassName('drill_cursor')[0];

addListener('click', element, function () {
    // Do stuff
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.