-1

Is there a method or function that does this, or do I have to check each character of a string. To give an idea on what I'm talking about, for example. I have an HTML TEXT like:

<p><strong>Welcome&nbsp;</strong>to this&nbsp;<em>message,&nbsp;</em><span style="text-decoration: underline;">you may come in peace.</span></p>

I need to convert it into a plain text, resulting into:

Welcome to this message, you may come in peace.

The text will come from a textarea which is a child of a div with an id = editor-email.

I also wanted to take the current text, but it won't work.

var textEmail = $('#editor-email').find('textarea').text();
Jan Ariel San Jose
  • 690
  • 3
  • 11
  • 31
  • ``` html = "

    Welcome to this message, you may come in peace.

    " html = html.replace(/
    – Dmytro Myronenko Apr 22 '21 at 08:12

3 Answers3

4

You can do it like with pure JS

let a = `<p><strong>Welcome&nbsp;</strong>to this&nbsp;<em>message,&nbsp;</em><span style="text-decoration: underline;">you may come in peace.</span></p>
`;

let d = document.createElement('div');
d.innerHTML = a;
console.log(d.innerText);
vibhor1997a
  • 2,336
  • 2
  • 17
  • 37
0

I think you need:

$("p").text()

EDIT:

If you want the value from the textarea then you will need:

$("#editor-email > textarea").val();
Kees
  • 1,408
  • 1
  • 15
  • 27
0

If you're looking for a JQuery-free solution, you can select the element and use .innerText:

document.querySelector('#myElement').innerText
Antony
  • 1,253
  • 11
  • 19