Click to See Complete Forum and Search --> : innerText problem


spoonshoe
07-21-2003, 09:42 PM
Hi...I am new at this ....i am trying to get this to roll several random numbers and write those into the html......HELP!!!!


<html>
<head>

<script language='javascript'>

function Change()
{

for(i=0;i<3 ;i++)
{

var holder="Hair"+i;
"+holder+".innerText=Math.round(Math.random() * 6);
alert(holder);
}
}

</script>

</head>
<body>

<div style='position:absolute; left:0px; top:0px;'>
<input type='button' value='Change' onclick='Change();'>
</div>

<table border=1 cellspacing=0 cellpadding=0 style='position:absolute; top:150px; left:10px; '>

<tr>
<th>Rolled</th>
</tr>

<tr>
<td><div id=Hair0>0</div></td>
</tr>
<tr>
<td><div id=Hair1>0</div></td>
</tr>
<tr>
<td><div id=Hair2>0</div></td>
</tr>

</table>
</body>
</html>

pyro
07-21-2003, 09:48 PM
This will not only work, but give you better cross-browser results (as innerText is IE only...) I also used <script type="text/javascript"> as the language attribute has been depreciated for quite some time, now...

The trick was using document.getElementById to reference the ID of the division element...

<html>
<head>

<script type="text/javascript">

function Change() {

for(i=0;i<3 ;i++) {

var holder="Hair"+i;
document.getElementById(holder).innerHTML=Math.round(Math.random() * 6);
alert(holder);
}
}

</script>

</head>
<body>

<div style='position:absolute; left:0px; top:0px;'>
<input type='button' value='Change' onclick='Change();'>
</div>

<table border=1 cellspacing=0 cellpadding=0 style='position:absolute; top:150px; left:10px; '>

<tr>
<th>Rolled</th>
</tr>

<tr>
<td><div id="Hair0">0</div></td>
</tr>
<tr>
<td><div id="Hair1">0</div></td>
</tr>
<tr>
<td><div id="Hair2">0</div></td>
</tr>

</table>
</body>
</html>

Edward Files
07-21-2003, 11:40 PM
I entered your code in a html editor and it worked ok but for one thing; I could not exit the page. I had to shut down the editor in order to exit. Perhaps using this code by itself causes you to be in a never ending loop. Do you know what the code is being used for other than picking random numbers?

pyro
07-22-2003, 07:05 AM
No, it doesn't put you in an endless loop. It works fine for me in IE6 and Mozilla 1.2. Did you get any error messages?