/    Sign up×
Community /Pin to ProfileBookmark

Looping through an array JS

I have an array of string and an h1 element. I am trying to replace the innerHTML of the h1 element with a string from the array in the order that the strings are placed in the array.
I am using a for loop but it seems like the index does not increment the way I wanted. I can’t access each individual index in order. It loops through the array very fast and give’s me the last index of the array.

Here’s the code pen:
https://codepen.io/raul-rogojan/pen/VwbmaLL

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@daveyerwinJul 12.2021 — @RaulRogojan#1634029

``<i>
</i>&lt;h1 class="text"&gt;&lt;/h1&gt;
&lt;button class="button"&gt;click me&lt;/button&gt;
&lt;script&gt;
"use strict"
var arr = ['Hello world!','How are you?', 'My name is John', 'Have a nice day'];
const text = document.querySelector('.text');
const button = document.querySelector('.button');
text.textContent = arr.shift();
function changeH1WithDelay(){
if(arr.length)text.textContent = arr.shift();
setTimeout(changeH1WithDelay,1000);
}
button.addEventListener('click', (e) =&gt; {
e.target.style.display='none';
changeH1WithDelay();
})
&lt;/script&gt;<i>
</i>
``

Please ask for explanation of any code you do not understnd
Copy linkTweet thisAlerts:
@codyhillauthorJul 12.2021 — @DaveyErwin#1634034 Thank you!

But I want the change to happen only once when the button is clicked.

like

on load, the h1 should have the arr[0]

Click the button - h1 should have arr[1]

Click the button again - h1 should have arr[2]

Click the button again - h1 should have arr[3]

so on.
Copy linkTweet thisAlerts:
@daveyerwinJul 12.2021 — @RaulRogojan#1634035

``<i>
</i>&lt;h1 class="text"&gt;&lt;/h1&gt;
&lt;button class="button"&gt;click me&lt;/button&gt;
&lt;script&gt;
"use strict"
var arr = ['Hello world!','How are you?', 'My name is John', 'Have a nice day'];
const text = document.querySelector('.text');
const button = document.querySelector('.button');
text.textContent = arr.shift();
function changeH1WithClick(){
if(arr.length)text.textContent = arr.shift();
}
button.addEventListener('click',changeH1WithClick)
&lt;/script&gt;<i>
</i>
``
Copy linkTweet thisAlerts:
@codyhillauthorJul 12.2021 — @DaveyErwin#1634036 thank you, but the shift method removes the items from the array witch I don't want
Copy linkTweet thisAlerts:
@daveyerwinJul 12.2021 — @RaulRogojan#1634037

``<i>
</i>&lt;h1 class="text"&gt;&lt;/h1&gt;
&lt;button class="button"&gt;click me&lt;/button&gt;
&lt;script&gt;
"use strict"
var arr = ['Hello world!','How are you?', 'My name is John', 'Have a nice day'];
const text = document.querySelector('.text');
const button = document.querySelector('.button');
var changeH1PreserveArr = (function(){
var temp = arr.splice(0);
text.textContent=temp.shift();
return function(){
if(temp.length)text.textContent=temp.shift();
}
}());
button.addEventListener('click',changeH1PreserveArr);
&lt;/script&gt;<i>
</i>
``
×

Success!

Help @codyhill spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.13,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...