Forum Moderators: open

Message Too Old, No Replies

mouseover changing cell above

I want to have a mouseover change the background color of the cell above

         

tweilund

7:14 pm on Feb 1, 2005 (gmt 0)

10+ Year Member



Hey, I'm trying to set up a nav menu where when you move over an option the cell directly above it changes its background color. I'm sure this is probably easy, but I have nil programming skills.

Thanks for any help.

Bernard Marx

8:20 pm on Feb 1, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'd advise getting some programming skills as soon as possible.

Bernard Marx

8:25 pm on Feb 1, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just my little joke.

Try this below. It's basic, but it expresses the ideas.
You can navigate the table using element relationships
- a cell's row is the cell's parentNode.

You can get the index of rows/cells using the rowIndex/cellIndex properties.

There are better ways of handling events. The mouseovers/outs could be added dynamically, for instance. The script could be made more generic by not relying on the id of the table being hard-coded - but that would complicate the issue.

Use CSS in a stylesheet for any normal styling, then the highlight color can be removed by sending an empty string to the element's inline style object (as in the script below).

<html>
<head>
<title>What? No bananas?</title>
<script language="javascript" type="text/javascript">

CELL_HIGHLITE_COLOR = "#00FFFF"

function doCellAbove(cell,boolean)
{
var table = document.getElementById('tab');
var rowIndex = cell.parentNode.rowIndex-1;
var cellAbove = table.rows[rowIndex].cells[cell.cellIndex];
cellAbove.style.backgroundColor
= (boolean)? CELL_HIGHLITE_COLOR : "";
}
</script>
</head>

<body>

<table id="tab">
<tr>
<td>upper cell</td>
</tr>
<tr>
<td onmouseover="doCellAbove(this,1)"
onmouseout ="doCellAbove(this,0)">lower cell</td>
</tr>
</table>
</body>
</html>

tweilund

8:59 pm on Feb 1, 2005 (gmt 0)

10+ Year Member



Sweet. That works great. Thanks man, and I'll try to get me some o' them programming skills. :)

tweilund

9:50 pm on Feb 1, 2005 (gmt 0)

10+ Year Member



Do you have any idea why it doesn't work in mozilla?

lZakl

11:26 pm on Feb 1, 2005 (gmt 0)

10+ Year Member



wouldn't this work theoretically... I didn't test it. Out the door right now, let me know if it works..I'll check back tomorrow.. I don't know if all my tags are correct... Like I said it's not tested.

<table border="1">
<tr>
<td
onMouseOver="document.all.cell2.bgColor=0x00FFFF"
onMouseOut="document.all.cell2.bgColor=0xFFFFFF">
Cell One
</td>
<td id="cell2">
Cell Two
</td>
</tr>
</table>

P.S. I use the document.all just to make sure I get the cell I am looking for..

-- Zak

Bernard Marx

11:30 pm on Feb 1, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



ah yes.

I used the name 'boolean' for 2nd the argument. It is in fact a number. This was simply to save space. It is evaluated as boolean {1:true, 0:false}.

Thing is, by using the word 'boolean', I have stumbled into some area of Mozilla Javascript that I haven't come across before. It's almost as if Firefox is supporting Javascript v2.

Still the solution is simple: Change the argument's name to something else eg 'highlite'.
(That'll be 2 changes). Then it's OK.

Bernard Marx

11:35 pm on Feb 1, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



lZakl, the code you've used targets a specific element, instead of being generic.
This means it can't be extended without hard-coding more ids.

More importantly, document.all only works in Internet Explorer (actually Opera too).

That would be DHTML mistake #4, I reckon ;)

tweilund

4:28 pm on Feb 2, 2005 (gmt 0)

10+ Year Member



Thanks Bernard, that got it.

lZakl

5:06 pm on Feb 2, 2005 (gmt 0)

10+ Year Member



Bernard,

tested, and behold -- the 'Mighty Marx' is right...

The doc.all isn't supported by Mozillla. I was unaware of this, but then again I've never used it. I just remembered reading about it ... hence the 'theoretically' in my previous post. :0)

-- Zak