Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

136
Visualizações
Splitting a paragraph into an array and animating each contents of the array on button click
<div id="container">
  <button id="button">Click!</button>
</div>

JS:

const container = document.querySelector("#container");
const button = document.querySelector("#button");
button.addEventListener("click", () => {
     let para = document.createElement("p");
     para.textContent = "Text message one. Text message two. Text message three.";
     let strText = para.textContent;
     let splitText = strText.split(".");
     
     for (let i=0; i<splitText.length;i++) {
     splitText.textContent += "<span>" + splitText[i] + "</span>";
     }
     container.appendChild(splitText[i]);
});

I AM STUCK! How can I write my code such that onclicking the button, the array components of the paragraph is shown(animated) on the div one after another ?? I know about the css animation and transition but I just don't know how to apply it here.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Do you mean something like this?

const container = document.querySelector("#container");
const button = document.querySelector("#button");

let idx = 0;
let sentnce = "Text message one. Text message two. Text message three.";
let texts = sentnce.split(".").slice(0, -1);

button.addEventListener("click", () => {
    if (idx < texts.length) {
        let p = document.createElement("p");
        p.textContent = texts[idx];
        container.appendChild(p);
        ++idx;
    }
});
<div id="container">
    <button id="button">Click!</button>
</div>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda