...

View Full Version : writing in div



gencoglu4
07-18-2007, 08:43 AM
Hi,
This script is supposed to write ten lines of randomly-generated numbers in <mydiv> and when the counter is grater than ten
I need to jump to <mydiv2> and keep writing...The problem is it is still writing <mydiv>
How can I disabled writing <mydiv> and keep writing <mydiv2> ?
thanks




<html>

<head>

<script>
var con=0;
var stopit=setInterval("yaz()",1000)
function yaz()
{

con++;
for(var m=1;m<=6;m++)
{
var rno=Math.floor(Math.random()*50)+1;


document.getElementById('mydiv').innerHTML+=rno+",";
document.getElementById('but1').value=con;
if(con>=10)
{

document.getElementById('mydiv2').innerHTML+=rno+",";
}
if(con>=20)
{

clearInterval(stopit)
}
}
document.getElementById('mydiv').innerHTML+="<br>";
document.getElementById('mydiv2').innerHTML+="<br>";
}

</script>
</head>

<body bgcolor=silver>

<input type=button id=but1 value="lets start" onClick="yaz()">


<div id="mydiv" style="position:absolute;top:50;left:20;background-color:white;">
<div id="mydiv2" style="position:absolute;top:50;left:200;background-color:pink;">

</body>
</html>

mcjwb
07-18-2007, 07:59 PM
If you only want 10 lines in mydiv you need another if statement at the beginning, like this;

<html>
<head>

<script type="text/javascript">
var con=0;
var stopit=setInterval("yaz()",1000)
function yaz()
{
var rno;
var but1 = document.getElementById('but1');
var mydiv = document.getElementById('mydiv');
var mydiv2 = document.getElementById('mydiv2');
con++;
for(var m=1;m<=6;m++)
{
but1.value=con;
rno=Math.floor(Math.random()*50)+1;
if(con<10){
mydiv.innerHTML+=rno+",";
}
else if(con>=10 && con<20)
{
mydiv2.innerHTML+=rno+",";
}
else if(con>=20)
{
clearInterval(stopit);
break; //might as well break out of the for loop now.
}
}
mydiv.innerHTML+="<br>";
mydiv2.innerHTML+="<br>";
}
</script>
</head>
<body bgcolor=silver>

<input type=button id=but1 value="lets start" onClick="yaz()">

<div id="mydiv" style="position:absolute;top:50;left:20;background-color:white;"></div>
<div id="mydiv2" style="position:absolute;top:50;left:200;background-color:pink;"></div>
</body>
</html>
I've also made it more efficient by assigning the references to the divs at the beginning of the function.

glenngv
07-18-2007, 08:27 PM
<html>
<head>
<script type="text/javascript">
var con=0;
var stopit=setInterval("yaz()",1000) ;
function yaz()
{
con++;
for(var m=1;m<=6;m++)
{
var rno=Math.floor(Math.random()*50)+1;
if (con>20) {
clearInterval(stopit) ;
return;
}
else if(con<=10)
{
document.getElementById('mydiv').innerHTML+=rno+",";
}
else if (con>10)
{
document.getElementById('mydiv2').innerHTML+=rno+",";
}
}
if(con<=10) document.getElementById('mydiv').innerHTML+="<br>";
else if (con>10) document.getElementById('mydiv2').innerHTML+="<br>";
document.getElementById('but1').value=con;
}

</script>
</head>

<body bgcolor=silver>

<input type=button id=but1 value="lets start" onClick="yaz()">

<div id="mydiv" style="position:absolute;top:50;left:20;background-color:white;"></div>
<div id="mydiv2" style="position:absolute;top:50;left:200;background-color:pink;"></div>

</body>
</html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum