pjordanna
asked on
CHANGE COLOR QUESTION (AGAIN...)
Hi experts,
I have the following code:
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
<!--
function colorMe(obj,oId){
var el = document.getElementById(ob j);
var e = document.getElementById(oI d);
if(document.getElementById ){
el.style.backgroundColor = 'red';
el.firstChild.style.color = 'yellow';
e.style.backgroundColor = '';
e.firstChild.style.color = '';
}
}
// -->
</script>
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td id="one"><a href="#" onclick="colorMe('one','tw o');return false;">TEST</a></td>
<td id="two"><a href="#" onclick="colorMe('two','on e');return false;">TEST 2</a></td>
</tr>
</table>
</body>
</html>
I also have 2 10 row tables of data (TABLE1 AND TABLE2). I am trying to get only one of them to be displayed based on which link (TEST OR TEST2) is clicked.
Basically the table needs to sit directly under the table which you see in the code above. If TEST is clicked then TABLE1 should be displayed. If TEST 2 is clicked then TABLE2 should be displayed.
I am looking for the same kind of effect that could be achieved by a "swapImage".
Is this possible?
PJORDANNA
I have the following code:
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
<!--
function colorMe(obj,oId){
var el = document.getElementById(ob
var e = document.getElementById(oI
if(document.getElementById
el.style.backgroundColor = 'red';
el.firstChild.style.color = 'yellow';
e.style.backgroundColor = '';
e.firstChild.style.color = '';
}
}
// -->
</script>
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td id="one"><a href="#" onclick="colorMe('one','tw
<td id="two"><a href="#" onclick="colorMe('two','on
</tr>
</table>
</body>
</html>
I also have 2 10 row tables of data (TABLE1 AND TABLE2). I am trying to get only one of them to be displayed based on which link (TEST OR TEST2) is clicked.
Basically the table needs to sit directly under the table which you see in the code above. If TEST is clicked then TABLE1 should be displayed. If TEST 2 is clicked then TABLE2 should be displayed.
I am looking for the same kind of effect that could be achieved by a "swapImage".
Is this possible?
PJORDANNA
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>>tbl.style.display="block ";<<
change to
tbl.style.display="";
it will display better
change to
tbl.style.display="";
it will display better
>>>Basically the table needs to sit directly under the table which you see in the code above.
========================== ========
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
<!--
function colorMe(obj,oId){
var el = document.getElementById(ob j);
var e = document.getElementById(oI d);
var tbl = document.getElementById(ob j+"tbl");
var tbl2 = document.getElementById(oI d+"tbl");
if(document.getElementById ){
el.style.backgroundColor = 'red';
el.firstChild.style.color = 'yellow';
tbl.style.display="block";
tbl2.style.display="none";
e.style.backgroundColor = '';
e.firstChild.style.color = '';
}
}
// -->
</script>
</head>
<body bgcolor=ffffff>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width=50% id="one"><a href="#" onclick="colorMe('one','tw o');return false;">TEST</a></td>
<td width=50% id="two"><a href="#" onclick="colorMe('two','on e');return false;">TEST 2</a></td>
</tr>
<tr>
<td>
<table style=display:none bgcolor=blue id=onetbl width=100% cellspacing=0 cellpadding=0 border=1>
<td>ONE</td>
</table>
</td>
<td>
<table style=display:none bgcolor=yellow id=twotbl width=100% cellspacing=0 cellpadding=0 border=1>
<td>TWO</td>
</table>
</td>
</tr>
</table>
<br><br>
</body>
</html>
==========================
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
<!--
function colorMe(obj,oId){
var el = document.getElementById(ob
var e = document.getElementById(oI
var tbl = document.getElementById(ob
var tbl2 = document.getElementById(oI
if(document.getElementById
el.style.backgroundColor = 'red';
el.firstChild.style.color = 'yellow';
tbl.style.display="block";
tbl2.style.display="none";
e.style.backgroundColor = '';
e.firstChild.style.color = '';
}
}
// -->
</script>
</head>
<body bgcolor=ffffff>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width=50% id="one"><a href="#" onclick="colorMe('one','tw
<td width=50% id="two"><a href="#" onclick="colorMe('two','on
</tr>
<tr>
<td>
<table style=display:none bgcolor=blue id=onetbl width=100% cellspacing=0 cellpadding=0 border=1>
<td>ONE</td>
</table>
</td>
<td>
<table style=display:none bgcolor=yellow id=twotbl width=100% cellspacing=0 cellpadding=0 border=1>
<td>TWO</td>
</table>
</td>
</tr>
</table>
<br><br>
</body>
</html>
This is for controlling more then two tables:
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
<!--
var aId = ["C1", "C2", "C3", "C4"];
function colorMe(theObj){
for(i=0;i<aId.length;i++){
document.getElementById(aI d[i]).styl e.backgrou ndColor='' ;
document.getElementById(aI d[i]).styl e.color='' ;
document.getElementById("d "+aId[i]). style.disp lay='none' ;
}
theObj.style.backgroundCol or='red';
theObj.style.color='yellow ';
document.getElementById("d "+theObj.i d).style.d isplay='bl ock';
}
// -->
</script>
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td id="C1" onclick="colorMe(this)">TE ST</td>
<td id="C2" onclick="colorMe(this)">TE ST 2</td>
<td id="C3" onclick="colorMe(this)">TE ST 3</td>
<td id="C4" onclick="colorMe(this)">TE ST 4</td>
</tr>
</table>
<div id="dC1" style="display:none">
<table>
<tr><td>A</td></tr>
</table>
</div>
<div id="dC2" style="display:none">
<table>
<tr><td>B</td></tr>
</table>
</div>
<div id="dC3" style="display:none">
<table>
<tr><td>C</td></tr>
</table>
</div>
<div id="dC4" style="display:none">
<table>
<tr><td>D</td></tr>
</table>
</div>
</body>
</html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
<!--
var aId = ["C1", "C2", "C3", "C4"];
function colorMe(theObj){
for(i=0;i<aId.length;i++){
document.getElementById(aI
document.getElementById(aI
document.getElementById("d
}
theObj.style.backgroundCol
theObj.style.color='yellow
document.getElementById("d
}
// -->
</script>
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td id="C1" onclick="colorMe(this)">TE
<td id="C2" onclick="colorMe(this)">TE
<td id="C3" onclick="colorMe(this)">TE
<td id="C4" onclick="colorMe(this)">TE
</tr>
</table>
<div id="dC1" style="display:none">
<table>
<tr><td>A</td></tr>
</table>
</div>
<div id="dC2" style="display:none">
<table>
<tr><td>B</td></tr>
</table>
</div>
<div id="dC3" style="display:none">
<table>
<tr><td>C</td></tr>
</table>
</div>
<div id="dC4" style="display:none">
<table>
<tr><td>D</td></tr>
</table>
</div>
</body>
</html>
Try this it works on tables of any sizes
<table width="200" id="tab1">
<tr>
<td bgcolor="blue">f</td>
<td bgcolor="pink">a</td>
<td>b</td>
</tr>
<tr>
<td bgcolor="red">f</td>
<td>c</td>
<td bgcolor="purple">d</td>
</tr>
<tr>
<td bgcolor="blue">f</td>
<td>a</td>
<td>b</td>
</tr>
</table>
<p><br>
</p>
<table id="tab2" width="200">
<tr>
<td bgcolor="orange">f</td>
<td bgcolor="green"></td>
<td>a</td>
</tr>
<tr>
<td bgcolor="blue">f</td>
<td>a</td>
<td>b</td>
</tr>
<tr>
<td bgcolor="red">c</td>
<td bgcolor="orange">c</td>
<td bgcolor="green">d</td>
</tr>
</table>
<a href="#" onClick="swap('tab1','tab2 ',3,3)">
<p>Swap Colors</a>
<script>
function swap(tab1,tab2,cols,rows){
for (i=0;i<cols;i++)
for (j=0;j<rows;j++)
{c1=document.getElementByI d(tab1).ro ws[i].cell s[j].bgCol or
c2=document.getElementById (tab2).row s[i].cells [j].bgColo r
document.getElementById(ta b2).rows[i ].cells[j] .bgColor =c1
document.getElementById(ta b1).rows[i ].cells[j] .bgColor =c2
}
}
</script>
<table width="200" id="tab1">
<tr>
<td bgcolor="blue">f</td>
<td bgcolor="pink">a</td>
<td>b</td>
</tr>
<tr>
<td bgcolor="red">f</td>
<td>c</td>
<td bgcolor="purple">d</td>
</tr>
<tr>
<td bgcolor="blue">f</td>
<td>a</td>
<td>b</td>
</tr>
</table>
<p><br>
</p>
<table id="tab2" width="200">
<tr>
<td bgcolor="orange">f</td>
<td bgcolor="green"></td>
<td>a</td>
</tr>
<tr>
<td bgcolor="blue">f</td>
<td>a</td>
<td>b</td>
</tr>
<tr>
<td bgcolor="red">c</td>
<td bgcolor="orange">c</td>
<td bgcolor="green">d</td>
</tr>
</table>
<a href="#" onClick="swap('tab1','tab2
<p>Swap Colors</a>
<script>
function swap(tab1,tab2,cols,rows){
for (i=0;i<cols;i++)
for (j=0;j<rows;j++)
{c1=document.getElementByI
c2=document.getElementById
document.getElementById(ta
document.getElementById(ta
}
}
</script>
again new try, without any id ;)
=======================
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script>
function colorMe(obj)
{
el=obj.parentNode.parentNo de;
n = el.firstChild.firstChild== obj?0:1;
el.nextSibling.childNodes( n).style.d isplay = "";
el.nextSibling.childNodes( n==0?1:0). style.disp lay = "none";
}
</script>
</head>
<body bgcolor=ffffff>
<table border="0" cellspacing="0" cellpadding="0">
<!-- one -->
<tr>
<td><a href="#" onclick="colorMe(this);ret urn false;">view table 1</a></td>
<td><a href="#" onclick="colorMe(this);ret urn false;">view table 2</a></td>
</tr>
<tr>
<td colspan=2 style=display:none>
<table width=100% cellspacing=0 cellpadding=0 border=1 bgcolor=red>
<td>TABLE1</td>
</table>
</td>
<td colspan=2 style=display:none>
<table width=100% cellspacing=0 cellpadding=0 border=1 bgcolor=green>
<td>TABLE2</td>
</table>
</td>
</tr>
<!-- /one -->
<!-- two -->
<tr>
<td><a href="#" onclick="colorMe(this);ret urn false;">view table 1</a></td>
<td><a href="#" onclick="colorMe(this);ret urn false;">view table 2</a></td>
</tr>
<tr>
<td colspan=2 style=display:none>
<table width=100% cellspacing=0 cellpadding=0 border=1 bgcolor=red>
<td>TABLE1</td>
</table>
</td>
<td colspan=2 style=display:none>
<table width=100% cellspacing=0 cellpadding=0 border=1 bgcolor=green>
<td>TABLE2</td>
</table>
</td>
</tr>
<!-- /two -->
<!-- ...... more...... -->
</table>
</body>
</html>
=======================
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script>
function colorMe(obj)
{
el=obj.parentNode.parentNo
n = el.firstChild.firstChild==
el.nextSibling.childNodes(
el.nextSibling.childNodes(
}
</script>
</head>
<body bgcolor=ffffff>
<table border="0" cellspacing="0" cellpadding="0">
<!-- one -->
<tr>
<td><a href="#" onclick="colorMe(this);ret
<td><a href="#" onclick="colorMe(this);ret
</tr>
<tr>
<td colspan=2 style=display:none>
<table width=100% cellspacing=0 cellpadding=0 border=1 bgcolor=red>
<td>TABLE1</td>
</table>
</td>
<td colspan=2 style=display:none>
<table width=100% cellspacing=0 cellpadding=0 border=1 bgcolor=green>
<td>TABLE2</td>
</table>
</td>
</tr>
<!-- /one -->
<!-- two -->
<tr>
<td><a href="#" onclick="colorMe(this);ret
<td><a href="#" onclick="colorMe(this);ret
</tr>
<tr>
<td colspan=2 style=display:none>
<table width=100% cellspacing=0 cellpadding=0 border=1 bgcolor=red>
<td>TABLE1</td>
</table>
</td>
<td colspan=2 style=display:none>
<table width=100% cellspacing=0 cellpadding=0 border=1 bgcolor=green>
<td>TABLE2</td>
</table>
</td>
</tr>
<!-- /two -->
<!-- ...... more...... -->
</table>
</body>
</html>
If you want the second table to be invisible then do this
<div style="display:none">
<table width="200" id="tab1">
<tr>
<td bgcolor="blue">f</td><td bgcolor="pink">a</td><td>b </td>
</tr>
<tr>
<td bgcolor="red">f</td><td>c< /td><td bgcolor="purple">d</td>
</tr>
<tr>
<td bgcolor="blue">f</td><td>a </td> <td>b</td>
</tr>
</table>
</div>
<table id="tab2" width="200" border=1>
<tr>
<td bgcolor="orange">f</td> <td bgcolor="green"></td> <td>a</td>
</tr>
<tr>
<td bgcolor="blue">f</td> <td>a</td> <td>b</td>
</tr>
<tr>
<td bgcolor="red">c</td><td bgcolor="orange">c</td><td bgcolor="green">d</td>
</tr>
</table>
<a href="#" onClick="swap('tab1','tab2 ',3,3)">
<p>Swap Colors</a>
<script>
function swap(tab1,tab2,cols,rows){
for (i=0;i<cols;i++)
for (j=0;j<rows;j++)
{c1=document.getElementByI d(tab1).ro ws[i].cell s[j].bgCol or
c2=document.getElementById (tab2).row s[i].cells [j].bgColo r
document.getElementById(ta b2).rows[i ].cells[j] .bgColor =c1
document.getElementById(ta b1).rows[i ].cells[j] .bgColor =c2
}
}
</script>
<div style="display:none">
<table width="200" id="tab1">
<tr>
<td bgcolor="blue">f</td><td bgcolor="pink">a</td><td>b
</tr>
<tr>
<td bgcolor="red">f</td><td>c<
</tr>
<tr>
<td bgcolor="blue">f</td><td>a
</tr>
</table>
</div>
<table id="tab2" width="200" border=1>
<tr>
<td bgcolor="orange">f</td> <td bgcolor="green"></td> <td>a</td>
</tr>
<tr>
<td bgcolor="blue">f</td> <td>a</td> <td>b</td>
</tr>
<tr>
<td bgcolor="red">c</td><td bgcolor="orange">c</td><td
</tr>
</table>
<a href="#" onClick="swap('tab1','tab2
<p>Swap Colors</a>
<script>
function swap(tab1,tab2,cols,rows){
for (i=0;i<cols;i++)
for (j=0;j<rows;j++)
{c1=document.getElementByI
c2=document.getElementById
document.getElementById(ta
document.getElementById(ta
}
}
</script>
This will also swap the HTML content of the cells,
<div style="display:none">
<table width="600" id="tab1" border=1 cellpaddding="6">
<tr width="200">
<td bgcolor="blue">AAAAA</td>< td bgcolor="pink">XXX</td><td >BBB</td>
</tr>
<tr width="200">
<td bgcolor="red">BBBB</td><td >BEER!</td ><td bgcolor="purple">d</td>
</tr>
<tr width="200">
<td bgcolor="blue">f</td><td>Y AWN</td> <td>BEER!</td>
</tr>
</table>
</div>
<table id="tab2" width="600" border=1 cellpaddding="6">
<tr width="200">
<td bgcolor="orange">ffff</td> <td bgcolor="green"></td> <td>Time for Bed</td>
</tr>
<tr width="200">
<td bgcolor="blue">I am tired</td> <td>a</td> <td>ZZZzzzzz</td>
</tr>
<tr width="200">
<td bgcolor="red">c</td><td bgcolor="orange">I like wine</td><td bgcolor="green">d</td>
</tr>
</table>
<a href="#" onClick="swap('tab1','tab2 ',3,3)">
<p>Swap Colors</a>
<script>
function swap(tab1,tab2,cols,rows){
for (i=0;i<cols;i++)
for (j=0;j<rows;j++)
{c1=document.getElementByI d(tab1).ro ws[i].cell s[j].bgCol or
c2=document.getElementById (tab2).row s[i].cells [j].bgColo r
document.getElementById(ta b2).rows[i ].cells[j] .bgColor =c1
document.getElementById(ta b1).rows[i ].cells[j] .bgColor =c2
h1=document.getElementById (tab1).row s[i].cells [j].innerH TML
h2=document.getElementById (tab2).row s[i].cells [j].innerH TML
document.getElementById(ta b2).rows[i ].cells[j] .innerHTML =h1
document.getElementById(ta b1).rows[i ].cells[j] .innerHTML =h2
}
}
</script>
<div style="display:none">
<table width="600" id="tab1" border=1 cellpaddding="6">
<tr width="200">
<td bgcolor="blue">AAAAA</td><
</tr>
<tr width="200">
<td bgcolor="red">BBBB</td><td
</tr>
<tr width="200">
<td bgcolor="blue">f</td><td>Y
</tr>
</table>
</div>
<table id="tab2" width="600" border=1 cellpaddding="6">
<tr width="200">
<td bgcolor="orange">ffff</td>
</tr>
<tr width="200">
<td bgcolor="blue">I am tired</td> <td>a</td> <td>ZZZzzzzz</td>
</tr>
<tr width="200">
<td bgcolor="red">c</td><td bgcolor="orange">I like wine</td><td bgcolor="green">d</td>
</tr>
</table>
<a href="#" onClick="swap('tab1','tab2
<p>Swap Colors</a>
<script>
function swap(tab1,tab2,cols,rows){
for (i=0;i<cols;i++)
for (j=0;j<rows;j++)
{c1=document.getElementByI
c2=document.getElementById
document.getElementById(ta
document.getElementById(ta
h1=document.getElementById
h2=document.getElementById
document.getElementById(ta
document.getElementById(ta
}
}
</script>
u can use it without ids:
==========================
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script>
var lastobj=false;
var e=false;
function colorMe(obj)
{
el = obj.parentNode;
if(el)
{
el.style.backgroundColor = 'red';
el.firstChild.style.color = 'ffffff';
}
if(e)
{
e.style.backgroundColor = '';
e.firstChild.style.color = '';
}
e=obj.parentNode;
}
</script>
</head>
<body bgcolor=ffffff>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><a href="#" onclick="colorMe(this);ret
<td><a href="#" onclick="colorMe(this);ret
</tr>
<tr>
<td><a href="#" onclick="colorMe(this);ret
<td><a href="#" onclick="colorMe(this);ret
</tr>
<tr>
<td><a href="#" onclick="colorMe(this);ret
<td><a href="#" onclick="colorMe(this);ret
</tr>
</table>
</body>
</html>