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.