Tomcatom
Tomcatom

Reputation: 365

Applying an event handler to elements through a loop in Javascript

I would like to add an Event Handler for all of my <td> tags. I've tried the following:

var allSquares = document.getElementsByTagName("td");

for (var i = 0, len = allSquares.length; i < len; i++){
allSquares[i].addEventListener('ondragover', allowDrop, false);

}

Anyone has an idea why it didn't work? Thanks ahead

Upvotes: 0

Views: 67

Answers (2)

Bergi
Bergi

Reputation: 664548

With the W3-method addEventListener you don't put an "on" before the event's name (like you do for Microsoft):

allSquares[i].addEventListener('dragover', allowDrop, false);

See Quirksmode's article on the two advanced event registration models.

Upvotes: 1

I Hate Lazy
I Hate Lazy

Reputation: 48761

Generally event names passed to addEventListener do not start with "on".

allSquares[i].addEventListener('dragover', allowDrop, false);

Upvotes: 2

Related Questions