+ 1
Js array
Hey guys i am having some problem and i need some help... well lets imagine that i have an array in JavaScript like this let array = ['one', 'two', 'three']; function GoForward(){ document.getElementById('jokes01').innerHTML = array[0]; } But i intend to show each element of the array when ever i click on a button but i cant find a solution of how to show each element on a click of the button i have tried to add 1 on the length of the button but it still doesn't go so please i need some help... pleasssse!
2 Answers
+ 1
If you want to show all elements of array then use
document.getElementById('jokes01'). innerHTML = array;
It displays all elements,
If you want display one by one on each click of then use a global variable i and increment its value on each click like:
var i=0;
let array = ['one', 'two', 'three'];
function GoForward(){
document.getElementById('jokes01').innerHTML = array[i];
i+=1;
//on going beyond array length then reset to i=0
}
You can use loops to show all elements one by one alternatively also...
+ 1
Thank you so much you actually did help me @JayaKrishna...