R
R
RushV2021-07-17 14:20:07
JavaScript
RushV, 2021-07-17 14:20:07

How to make alternate appearance of words in a row of text?

How to make text appear one by one in a row inside the h1 tag?
Here is the markup:

<div class="header-page">
<h1>раз два три</h1>
</div>


// JavaScript
    let strTitle = $(' h1').text();
    for(let key of strTitle.split('" ")){
        $('.banner-text h1').text(key);
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Sokolov, 2021-07-17
@RushV

You will need a reference to the element, and an array of words.
Keep the counter in the variable, it will determine the word from the set.
Will increase by 1 at each step. To get a valid index of an array element from it, take the remainder of the division %by the length of the array.

There are several things going on here:words[current++ % words.length]

  1. current++the counter increases by 1, but its value BEFORE the increase comes into operation
  2. current % words.lengthgives the remainder after dividing by the length of the array. If there are 3 elements in the array, the remainder will be 0, 1 or 2. Just the indices of the words in the array.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question