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

211
Vistas
How to trim html elements and class from string in JS

I have the following string and code:

str = `<p class="">Lores Ipsimulm</p><p class="">1 x 100ml  - 19% </p><p class="">1 x 100ml No.6 Mike</p><p class="">1 x 100ml No.3</p><`
var p = document.createElement("p");
p.textContent = str;
var converted = p.innerHTML;

document.getElementById("final").value = converted;

How do I trim all HTML elements including classes, their name, and other attributes?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

set with .innerHTML, get with .innerText

You can use innerText to get the text-only representation of an element's innerHTML. So you set the .innerHTML value and get the text using .innerText.
innerText will use the raw text with the exact spaces existing in the HTML. If you want to format the text for every p tag, you must loop through them.
See example snippet below for .innerText usage.

var str = `<p class="">Lores Ipsimulm</p><p class="">1 x 100ml - 19% </p><p class="">1 x 100ml No.6 Mike</p><p class="">1 x 100ml No.3</p>`;
var p = document.createElement("p");
p.innerHTML = str;
var converted = p.innerText;

console.log(converted);

You set the HTML value with innerHTML, but you get the text using innerText as shown in the snippet.

Your example HTML has an open < at the end. Make sure to use valid HTML.

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