PDA

View Full Version : innerHTML Not working!!



locbtran
10-15-2011, 06:08 PM
I trying to display a random number using innerHTML
Here's my code:

<html>
<head>
<script language="JavaScript">
var myArray = new Array("String1", "String2", "String3", "String4", "String5", "String6", "String7", "String8", "String9", "String10");

var num = Math.floor(myArray.length * Math.random());

document.writeln(myArray[num]);
//document.getElementById("myString").innerHTML = myArray[num];
</script>
</head>


<body>
<div id="myString"></div>
</body>
</html>

it works when I use document.writeln(myArray[num]); but does NOT work for document.getElementById("myString").innerHTML = myArray[num];

any ideas??
tks

jscheuer1
10-15-2011, 06:46 PM
The way you have it the target division doesn't exist . . . yet. This works:


<html>
<head>
</head>
<body>
<div id="myString"></div>
<script>
var myArray = new Array("String1", "String2", "String3", "String4", "String5", "String6", "String7", "String8", "String9", "String10");

var num = Math.floor(myArray.length * Math.random());

document.getElementById("myString").innerHTML = myArray[num];
</script>
</body>
</html>