Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the text between 2 elements

<div class="container">
  <a href="#" class="link">asd</a>
  [I want get this text]
  <a href="#" class="link">asd</a>
  Anouther text i dont need.
</div>

How can I withdraw the text between elements (not the inner text of elements, in this case I should get "[I want get this text]")?

like image 513
Fisher Avatar asked Jan 19 '12 20:01

Fisher


People also ask

How do I get text from an element?

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.

How to get text from an element jQuery?

You can simply use the jQuery text() method to get all the text content inside an element. The text() method also return the text content of child elements.


1 Answers

DEMO

I think the simplest way would be to deal with the native dom elements:

var a = $(".container a")[0];
var textNode = a.nextSibling;
var text = textNode.textContent;

Note that var text = textNode.nodeValue; will also work, but I'm not sure which is preferable.

like image 147
Adam Rackis Avatar answered Sep 19 '22 13:09

Adam Rackis