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

212
Visualizações
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 Respostas
Responde à pergunta

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 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