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

149
Vistas
How can I put individual words in <span> elements in a contenteditable <div>?

question

How do I put words in <span> elements in a contenteditable <div> while still keeping the cursor position? For example: if someone typed:
"the fox guy jumped over the dog", it would output:

<span>the</span> <span>fox</span> <span>guy</span>... 

attempt

document.querySelector("#container").addEventListener("input", function() {
    this.innerHTML = "<span>" + this.innerText
        .replaceAll("&", "&amp;") //escape HTML
        .replaceAll("<", "&lt;")
        .replaceAll(">", "&gt;")
        .split(" ")
        .join("</span><span>") + "</span>";
});

It didn't work because it didn't keep the cursor position, it just teleported the cursor to the end.

UPDATE:

I want to use the input event because I want it to split while the person is typing, and not when they click out.

about 4 years ago · Juan Pablo Isaza
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