I am practicing for course work and wanted to make a simple code to cycle through each string in an array. However, what actually happens is that it displays every string in the array over and over instead of replacing the one colour for another. How do I do this?
<!DOCTYPE html>
<html>
<head>
<title>Arrays</title>
<script>
colours = [" Purple"," Blue"," Pink"," Red"," Green"];
function change() {
for (i = 0; i < colours.length; i++) {
document.getElementById("hai").innerHTML += colours[i];
}
}
</script>
</head>
<body>
<p>The colour is: <span id=hai></span></p>
<button id="btn" onclick="change()">Change!</button>
</body>
</html>
colours = [" Purple", " Blue", " Pink", " Red", " Green"];
function change() {
for (i = 0; i < colours.length; i++) {
document.getElementById("hai").innerHTML += colours[i];
}
}
<p>The colour is: <span id=hai></span></p>
<button id="btn" onclick="change()">Change!</button>
changefunction, increment that pointer (remembering to make it go back to 0 at the end of the array) and display the corresponding array value.