Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select first text node in this markup

I'm trying to select the first text node in the first li in this ordered list...

<ol id="authors">
  <li id="CD007786-cr-0002">Robert S Phillips<sup>1,*</sup>, </li>
  <li id="CD007786-cr-0003">Shireen Gopaul<sup>2</sup>, </li><li id="CD007786-cr-0004">Faith Gibson<sup>3</sup>, </li>

So far I have tried:

jQuery("ol#authors li:first-child").text()

But this returns:

Robert S Phillips1,*, 

What I am trying to get is simply:

Robert S Phillips
like image 555
robotrobot Avatar asked Dec 14 '11 15:12

robotrobot


People also ask

How to select text node?

The textNodes of any element can be selected using jQuery by selecting all the nodes and using the filter() method to check the nodeType property. The required element is first selected using the jQuery selector. The contents() method is used on selected elements.

How do you find the text node of an element?

text(); This gets the contents of the selected element, and applies a filter function to it. The filter function returns only text nodes (i.e. those nodes with nodeType == Node. TEXT_NODE ).

What is a text node?

A text node encapsulates XML character content. A text node can have zero or one parent. The content of a text node can be empty. However, unless the parent of a text node is empty, the content of the text node cannot be an empty string.

How do you get text tags in HTML?

Use the textContent property to get the text of an html element, e.g. const text = box. textContent . The textContent property returns the text content of the element and its descendants. If the element is empty, an empty string is returned.


1 Answers

Try

alert( jQuery("#authors li:first-child").contents().get(0).nodeValue );
like image 59
Kevin B Avatar answered Oct 07 '22 00:10

Kevin B