I'm trying to print an array to a textarea in JavaScript. My current code only prints the first element instead of all four, and I don't know why.I have tried using a regular for loop as well, but that doesn't make a difference.
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
var listArray = function()
{
var people = ["Ben", "Joel", "Mary", "Tina"];
var scores = [88, 98, 100, 78];
for (var key in people)
{
var obj = people[key];
var num = scores[key];
var string = obj + ", " + num + "\n";
document.getElementById("box").innerHTML = string;
}
}
window.onload = function()
{
document.getElementById("show_score").onclick = showHighScore;
document.getElementById("list_array").onclick = listArray;
}
</script>
</head>
<body>
<form id="high_score" name="high_score" action="highScore.html" method="get">
<label>Results</label>
<br>
<textarea cols="50" rows="4" id="box"></textarea>
<br>
<input type="button" value="List Array" id="list_array" onclick="listArray">
<br>
<input type="button" value="Show Best Score" id="show_score" onclick="showHighScore">
</form>
</body>
</html>
.innerHTML = ...will overwrite..innerHTML += ...appends. Also noteshowHighScoreisn't defined.