Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

135
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda