Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

210
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!