Click to See Complete Forum and Search --> : document.getElementById problem.... I think


DJRobThaMan
08-11-2003, 02:21 PM
What's up,


I'm trying to get this to work so that when you click on one div the text within it says "on" and the div that previously had "on" changes to "off".

If anybody can figure out where I went wrong that would be great.


<html>
<head>
<script type="text/JavaScript">
<!--
var i;
var j;
var k:

function tabs(i){
var thing = eval('document.getElementById(' + i + ').innerHTML');
thing = "on";
}

function lasttab(k){
var stuff = eval('document.getElementById(' + j + ').innerHTML');
j = 1;
while(j < 6){
if(stuff == "on"){
if(j == k){
}
else
{
stuff = "off";
}
}
++j;
}
}







-->
</script>
</head>
<body>
<div id="1" onClick="tabs(1);lasttab(1);" border=1 style="background-color:red;position:absolute;left:10;top:10;width:100;height:50;">
on
</div>
<div id="2" onClick="tabs(2);lasttab(2);" border=1 style="background-color:blue;position:absolute;left:110;top:10;width:100;height:50;">
off
</div>
<div id="3" onClick="tabs(3);lasttab(3);" border=1 style="background-color:red;position:absolute;left:210;top:10;width:100;height:50;">
off
</div>
<div id="4" onClick="tabs(4);lasttab(4);" border=1 style="background-color:blue;position:absolute;left:310;top:10;width:100;height:50;">
off
</div>
<div id="5" onClick="tabs(5);lasttab(5);" border=1 style="background-color:red;position:absolute;left:410;top:10;width:100;height:50;">
off
</div>




</body>
</html>


Thanks

Fang
08-11-2003, 03:17 PM
This is the onclick function with global variable:

var IamON=1;
function ChangeText(i){
if(document.getElementById( "div"+i ).innerHTML!="on") {
document.getElementById( "div"+IamON ).innerHTML="off"
document.getElementById( "div"+i ).innerHTML="on"
IamON=i;
}
}

<div id="div1" onClick="ChangeText(1);" border=1 style="background:red;position:absolute;left:10;top:10;width:100;height:50;">on</div>

An "id" should begin with a character, not a number.