user1743430
user1743430

Reputation:

Onclick event handler by window.onload doesn't trigger

I want to call the function "details" when an element with the class "link" is clicked. This code doesn't work:

window.onload = function () {
    document.getElementById("link").onclick = details;
}

How can I do this?

Upvotes: 1

Views: 180

Answers (1)

Trott
Trott

Reputation: 70115

document.getElementById("link") will select an element with an id of "link", not an element with a class of "link".

Depending on what browsers you need to support, you might use document.getElementsByClassName() instead. (Won't work in IE8 or below, but works in all other browsers you are likely to care about.) You'll have to assign the click handler to each element that is returned.

Upvotes: 1

Related Questions