2

I get text field in the document. I get it by Jquery. But sometimes text field includes <em> </em> or <cite> </cite> tags. I want the raw text.

I get the text by their id such as; $('title')[0].innerHTML

It returns me;

further cholesterol transport <em class="a-plus-plus"> Large luteal cells </em> produce  <em class="a-plus-plus">more</em>

or

<cite>Sodium bicarbonate</cite> is a white solid that 

How can I get the raw text as;

further cholesterol transport Large luteal cells produce more

Sodium bicarbonate is a white solid that 

Should I parse the text when I get by their id, or should write a regex that detects cite and em tags and gets the text inside these tags. What is the best solution for this problem? Is there any solution offered for this type of parsing?

4 Answers4

4

The jQuery method .text() will get the text content of the selector, which will exclude HTML.

$('title').text();

Similarly in plain JavaScript the .textContent property will also do this by not parsing as HTML.

$('title')[0].textContent;
Spencer Wieczorek
  • 21,229
  • 7
  • 44
  • 54
1

A. Wolff is right. Only

$(container).text()

will work in JQuery.

You can see the reference Get the pure text without HTML element by javascript? It is pretty much what you have asked for.

Community
  • 1
  • 1
0

Just try to use:

$('title').text();
Naman
  • 1,519
  • 18
  • 32
Dresha
  • 96
  • 1
  • 5
0

In jquery

$(container).text()

Native Javascript

document.getElementById('container').innerText.
Piyush.kapoor
  • 6,715
  • 1
  • 21
  • 21