Forum Moderators: open

Message Too Old, No Replies

Problems with onMouseOver with FireFox

         

cellery

12:48 am on Mar 14, 2008 (gmt 0)

10+ Year Member



Ok so i have a table with six elements. When the mouse hovers over an element it turns that element a gray, and the other five elements a darker gray. The effect is nice, but does not work with FireFox

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>

MarkFilipak

2:30 am on Mar 14, 2008 (gmt 0)

10+ Year Member



1 - Use onmouseover, not onMouseOver -- don't be fooled by Microsquish documentation.
2 - Color numbers must be preceded by a hash sign (#) as in '#686868'.
3 - The delimiter for javascript statements is semi-colon (;), not comma (,).
4 - You should use backgroundColor rather than background if you want to change background-color in javascript.
Other than those small errors, it looks OK.

cellery

2:49 am on Mar 14, 2008 (gmt 0)

10+ Year Member



ok thanks, ill try that out and hope for the best

cellery

3:14 am on Mar 14, 2008 (gmt 0)

10+ Year Member



well i did everything, but firefox still is not working properly, i tried doing an alert(bodies[0]) and it says that it is undefined, i then proceeded to try that with IE and it said [object], so i guess firefox does not allow arrays of ids? I'm really unsure, do you have any ideas?

cellery

3:44 am on Mar 14, 2008 (gmt 0)

10+ Year Member



Ok well i actually found a way to solve my problem. I just gave each element its own unique ID so i wouldnt have to deal with arrays of the same ID. It's not very efficient coding but i dont see another way and now it works for IE and FireFox

MarkFilipak

3:55 am on Mar 14, 2008 (gmt 0)

10+ Year Member



Can you show me the code that populates bodies? If bodies[0] is undefined, then that array member doesn't exist.

> 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]

MarkFilipak

4:03 am on Mar 14, 2008 (gmt 0)

10+ Year Member



> I just gave each element its own unique ID

Of course. You have to do that. You also have to populate your bodies array somehow. How well do you know javascript?

httpwebwitch

6:20 am on Mar 14, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



putting an ID on every element is overkill... but who complains about overkill when the bug is dead?

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.

cellery

6:26 am on Mar 14, 2008 (gmt 0)

10+ Year Member



Unforunately i do not have too much experience with Javascript, i base a lot of stuff from other languages that i have learned from, i need to devote some time to learning more about javascript, but alas, i dont really have the time lol

cellery

6:36 am on Mar 14, 2008 (gmt 0)

10+ Year Member



to briefly explain what i was doing, i was giving the same id name to each of my table elements, ie
<tr>
<td id=bodies>Text </td>
<td id=bodies>More text </td>
</tr>
etc. and i was trying to access each table element by bodies[some int].properties, i didnt actually know if i could do that, but i assumed that by giving different table elements the same id, i was creating an array with each element referring to the table element object. It worked when i tried it with IE, so i assumed i was doing everything correctly.

fside

1:11 pm on Mar 14, 2008 (gmt 0)

10+ Year Member



I would just make a suggestion. In future, use different names for the, id, attribute. If you wanted to name things the same, for some reason, just use an attribute of your own, like "id2", or "cid" or whatever.

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.

MarkFilipak

11:30 pm on Mar 14, 2008 (gmt 0)

10+ Year Member



You need to learn javascript. But take heart. It's not that different from PHP, but has more of the C language syntax. (It's quite different from Perl, though.)

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')

succeeds for cells, otherwise, it fails.

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])

where doCell is a function and the argument is the actual cell (i.e., pass by value, not reference).

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.

MarkFilipak

12:35 am on Mar 15, 2008 (gmt 0)

10+ Year Member



If you copy the code above, you will need to replace the bogus pipe char (¦) with a real pipe char. This forum software seems to screw around with postings a little more than I thought it did. Plain text would be great, wouldn't it?