Jump to content

<a onmouseover doesn't make any difference!


Recommended Posts

I'm flummoxed!I have this code to loop through all the links on my page (and eventually add popup notices):

for (var i = 0; i < document.getElementsByTagName("a").length; i ++) {with (document.getElementsByTagName("a")[i]) {onmouseover = "alert('Test');";}}

The href and style properties works, but event properties seem to make no difference to to behavior of the web page.What did I do wrong?

Link to comment
Share on other sites

First, forget about the with statement; it's unnecessary and expensive. And try this:

			var anchors = document.getElementsByTagName("a");			for (var i = 0; i < anchors.length; i++) {				anchors[i].onmouseover = function(){						alert('Test');					};			}

From what I gather, the problem which you linked to could have been solved without that much complexity; I think they just needed to pass the event as a parameter for Firefox, not (basically) eval() a string.

Link to comment
Share on other sites

as Jesdisciple has pointed out you have to assign a function to an event. You cannot assign a string.Either use the example already provided or you could also do it this way

function assignHandler() {  alert('Test');}var anchors = document.getElementsByTagName("a");for (var i = 0; i < anchors.length; i++) {  anchors[i].onmouseover = assignHandler;}

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...