Forum Moderators: open
Heres the code from one of the six table elements, i guess what i am trying to ask is how do i access the background color of a table element in firefox. I hope the code is not too confusing
<tr><td id="bodies" onMouseOver="bodies[0].style.background='686868', bodies[1].style.background='686868', bodies[2].style.background='686868', bodies[3].style.background='686868', bodies[4].style.background='686868', bodies[5].style.background='8e8d8d'"
onMouseOut="bodies[0].style.background='black', bodies[1].style.background='black',
bodies[2].style.background='black', bodies[3].style.background='black', bodies[4].style.background='black', bodies[5].style.background='black'"> </td></tr>
> so i guess firefox does not allow arrays of ids
Ids are strings. Of course FF allows arrays of strings (or anything else: arrays of booleans, arrays of numbers, arrays of objects, arrays of functions, etc. plus arrays of mixed datatypes without restriction)
FYI,
You create indexed arrays in javascript with
myIndexedArray = [thingie0, thingie1, thingie2, ...]
where thingies can be strings, numbers, objects, mixed up... anything. The members of the array are accessed by
thingie = myIndexedArray[i]
where i is an integer.
You create associative arrays in javascript with
myAssociativeArray = {name: thingie, name: thingie, name: thingie}
where name is a unique name for each thingie.
The associative array is actually an object and the names are actually added as object properties -- so if name == 'myThingie' for example, then
thingie = myAssociativeArray[name] or
thingie = myAssociativeArray['myThingie'] or
thingie = myAssociativeArray.myThingie
will all work.
When you wrote that "so i guess firefox does not allow arrays of ids" I can't tell whether you mean this:
element = document.getElementById(bodies[i])
or this:
element = bodies[id]
The first is an indexed array and the second is an associative array.
Edit: Completed uncompleted thought.
[edited by: MarkFilipak at 3:56 am (utc) on Mar. 14, 2008]
Another method: you can access the <td> cells as element.childNodes where element is the <tr>. childNodes [w3schools.com] is a collection you can access like an array.
The table elements ARE ordered, but are simply nested under the parent element. There's no array. Logically, yes, it's an ordered list or array, if you like. Depends on how you look at it. But to speak of an array as a script object, you'd have to create an array in javascript, if for some reason you wished to do so for this sort of thing.
I figured you knew other languages. For example, the way you thought the id attribute worked. Javascript is not that smart. That's good and bad -- you have to create more code, but it's a lot more predicable.
OK, education time:
Suppose you have a table row with id='tr3'. You can access it with document.getElementById('tr3'). The cells in that row are its children, however, not all of its children are necessarily cells. Some of its children may be text nodes. You can tell a text node from an HTML element by a property named "tagName". For example:
if (document.getElementById('tr3').childNodes[0].tagName == 'TD') So you can do a loop:
for (var C, i=0, I=(C=document.getElementById('tr3').childNodes).length; I--; i++)
if (C[i].tagName=='TD') doCell(C[i]) There are some wrinkles. First 'TD' might be 'td', so the above must be made case-insensitive. Second, there is another type of cell, 'TH' (or 'th'). The following, using regular expressions, covers them all.
for (var C, i=0, I=(C=document.getElementById('tr3').childNodes).length; I--; i++)
if (C[i].tagName.search(/^td$¦^th$/i)+1) doCell(C[i]) I hope this is enough to get you a little bit further.