PDA

View Full Version : change bgColor on all TR's


jcma
01-28-2003, 11:09 AM
i have a table and when a user clicks a tr it changes color.

this works fine. The problem is that the user can only have one row selected.
so what i want to do is when onClick e cought it will go and change all tr's to white(default) and then change back the color on the selected one.

i have something like this:


<tr onClick="clicou(this);" id="linha" name="linha">


function clicou(obj)
{
obj.bgColor = "#B0C4DE";
}



how do i change all the tr's bgColor? IE and NS!
thanks

jcma
01-28-2003, 11:39 AM
on other words,
how do i select a document <TR> ??

on NS??

on IE is something like document.all.TRID.
but how is it on NS >= 6 ??

Danne
01-28-2003, 12:28 PM
Your code works in IE5.5 and NS6.1


This is what I used to test:



<html>
<head>
<title> Título </title>
</head>
<script language="javascript">

function clicou(obj)
{
obj.bgColor = "#B0C4DE";
//obj.style.backgroundColor
}

</script>
<body onload="">
<table border=0 cellspacing=5 cellpadding=0>
<tr onClick="clicou(this);" id="linha" name="linha">
<td>sdfdsf</td>
<td>sdfsdf</td>
</tr>
<tr>
<td>sdfsdf</td>
<td>sdfsdfsdf</td>
</tr>
</table>
</body>
</html>



The bgColor is stated as depricated in IE, thought. Use TR.style.backgroundColor instead.

jcma
01-28-2003, 12:33 PM
thanks for the help but that was already working.

what i want to know is to access other TR's that are not called with "clicou(this)".

imagine that i have 2 tr's (TR1 and TR2).

i click on TR1 and it changes color. then i click on TR2 and want TR1 to change back to the default color and change TR2 to the "clicked " color.

o hope you got it.

thanks

Danne
01-28-2003, 01:06 PM
ok, so try this instead:



<html>
<head>
<title> Título </title>
</head>
<script language="javascript">


function clicou(obj)
{
if(selectedTR!=-1)
selectedTR.style.backgroundColor = "#FFFFFF";
selectedTR=obj;
selectedTR.style.backgroundColor = "#B0C4DE";
}

var selectedTR=-1; // Store the currently selected TR

</script>
<body onload="">

<table border=0 cellspacing=5 cellpadding=0>
<tr onClick="clicou(this);">
<td>sdfdsf</td>
<td>sdfsdf</td>
</tr>
<tr onClick="clicou(this);">
<td>sdfsdf</td>
<td>sdfsdfsdf</td>
</tr>
</table>
</body>
</html>


Hope that's what you were looking for...:)

jcma
01-28-2003, 01:24 PM
it works perfectly... i didn't remember to store the object... that's great...

thanks alot :thumbsup:

obrigado :)

jcma
01-28-2003, 01:28 PM
just one thing, does the selectedTR.style.backgroundColor work on NS601 or do i have to use selectedTR.bgColor ?????

king443
01-28-2003, 02:48 PM
I tried this in NS7.0 - works fine.

Danne
01-28-2003, 03:10 PM
NS601 too...


até...

jcma
01-28-2003, 03:12 PM
thanks.....