PDA

View Full Version : Javascript: DOM Issue or bad code?



denizengt
November 6th, 2006, 12:34 AM
function mouseOver() {

if (!document.body.getElementsByTagName){ return; }
var allfields = new Array();
allDivs = document.body.getElementsByTagName("DIV");
for (var i=0; i<allDivs.length; i++){
var field = allDivs[i];
if (field.getAttribute('class') == "greyBox") {
field.onmouseover = function () {this.className = 'greyBoxHover';}
field.onmouseout = function () {this.className = 'greyBox';}
}
}
}

I wrote this function to change the background of DIV's going by the class "greyBox". When the page loads it is initialised - it works in Firefox, but doesn't in IE. As far as I know, getElementsByTagName is supported in both browsers - and I've tested for null returns.

Does anyone know why it doesn't work in IE (5.5, 6)?

matthewjumps
November 6th, 2006, 12:38 AM
i kno crap all but if your defining the variable allDivs

allDivs = document.body.getElementsByTagName("DIV");

it might be better to do

var allDivs = document.body.getElementsByTagName("DIV");
???


hey another aussie...and a fellow adelaidean no less ... what area u in? im at St Agnes, near TTP...

denizengt
November 6th, 2006, 01:06 AM
No luck :(


How do you do the offtopic shindigger?
I'm at Sheidow Park down south man. Used to live in Holden Hill

matthewjumps
November 6th, 2006, 01:13 AM
[ ot ] and [ / ot ] without all the spaces...

simplistik
November 6th, 2006, 02:46 AM
function mouseOver() {
var mo = document.getElementsByTagName("*");
for (var i=0; i<mo.length; i++) {
var dv = mo[i];
if (String(" "+dv.className+" ").indexOf(" greyBox ") > -1) {
dv.onmouseover = function () { this.className = 'greyBoxHover'; }
dv.onmouseout = function () { this.className = 'greyBox'; }
}
}
}


<script type="text/javascript">mouseOver();</script>

denizengt
November 6th, 2006, 02:54 AM
Much simpler, and it works.

Thanks a lot simplistik

simplistik
November 6th, 2006, 12:11 PM
sure thing brotha... was gonna make it inline... but well... ya know ;) :lol:

simplistik
November 6th, 2006, 04:33 PM
:lol: Just as a side note... this code as originally given to me by icio :D and I just adapted it to your thinga ma bob

icio
November 6th, 2006, 04:33 PM
ZOMGOSH YOU SO OWN!!

simplistik
November 6th, 2006, 04:35 PM
ZOMGOSH YOU SO OWN!!ONOZ!!!