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
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.
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 ).
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.
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.
Try
alert( jQuery("#authors li:first-child").contents().get(0).nodeValue );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With