programmer-in-training Posted May 29, 2008 Share Posted May 29, 2008 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 More sharing options...
programmer-in-training Posted May 29, 2008 Author Share Posted May 29, 2008 Is this what I have to do? Link to comment Share on other sites More sharing options...
Jesdisciple Posted May 29, 2008 Share Posted May 29, 2008 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 More sharing options...
aspnetguy Posted May 29, 2008 Share Posted May 29, 2008 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 More sharing options...
programmer-in-training Posted May 29, 2008 Author Share Posted May 29, 2008 Does that work for both IE and FF? Link to comment Share on other sites More sharing options...
aspnetguy Posted May 29, 2008 Share Posted May 29, 2008 Does that work for both IE and FF?yes it does Link to comment Share on other sites More sharing options...
programmer-in-training Posted May 29, 2008 Author Share Posted May 29, 2008 Great! I would have thought that you could assign strings as well, because you assign strings in HTML. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.