Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get only direct text without tags with jQuery in HTML

Tags:

html

jquery

I've got an HTML:

<strong>1)</strong>TEXT THAT I ONLY NEED<p>some par</p><ul>..</ul>

I need only "TEXT THAT I ONLY NEED" that is not withing any tags in his HTML, how can I get it with jQuery?

like image 667
WHITECOLOR Avatar asked Dec 24 '11 12:12

WHITECOLOR


People also ask

How do I get just the text from HTML in jQuery?

The best way is to . clone() your object, . remove() all of its . children() , then go back to the object using .

How do I get text only 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.

Is text () a jQuery method?

jQuery text() MethodThe text() method sets or returns the text content of the selected elements. When this method is used to return content, it returns the text content of all matched elements (HTML markup will be removed). When this method is used to set content, it overwrites the content of ALL matched elements.

What is $() in jQuery?

$() = window. jQuery() $()/jQuery() is a selector function that selects DOM elements. Most of the time you will need to start with $() function. It is advisable to use jQuery after DOM is loaded fully.


4 Answers

The best way is to .clone() your object, .remove() all of its .children(), then go back to the object using .end() and finally get the .text().

The method is described in this blog post.

$("strong")
        .clone()    //clone the element
        .children() //select all the children
        .remove()   //remove all the children
        .end()  //again go back to selected element
        .text();    //get the text of element
like image 54
Delgan Avatar answered Oct 13 '22 05:10

Delgan


This is tricky, because the text is in a text node, and jQuery doesn't support text nodes, only elements. Also because different browsers treat white space in HTML code differently, so you end up with different sets of nodes in different browsers. You can't just get the nodes and rely on that the text node that you want is at the same index all the time.

You can use jQuery to locate an element, and use the DOM to get all the nodes inside it.

Example HTML:

<div id="test">
  <strong>1)</strong>
  TEXT THAT I ONLY NEED
  <p>some par</p>
  <ul>
    <li>asdf</li>
    <li>qwerty</li>
  </ul>
</div>

Use jQuery to find the div, use [0] to get the DOM element out of the jQuery object, and the childNodes property to get its children:

var nodes = $('#test')[0].childNodes;

Then you can use jQuery to loop through the nodes to find the node that is the strong element:

var index;
$.each(nodes, function(i,e){
  if (e.tagName == 'STRONG') {
    index = i;
    return false;
  }
});

Now you can use the DOM to get the text value from the next node:

var text = nodes[index + 1].nodeValue;
like image 29
Guffa Avatar answered Oct 13 '22 04:10

Guffa


well, if you use jQuery, you would do

$('.selector').text();

more info and examples here

edit: I just saw you want the text that is not in any tags - well this is not directly possible (fact is, that every text inside a html document is inside some HTML tags.)

In this case I would try to change the markup (easiest way), so the desired text is within html tags. If this is not possible, you use the above mentioned text() method to get the whole text from the parent element, and after you have to subtract the pieces you dont want.

see http://jsfiddle.net/3SSAR/1/ for an example of how to subtract the strings..

just remember to use substr and not - to subtract strings in javascript

like image 3
tmaximini Avatar answered Oct 13 '22 04:10

tmaximini


This code works for me:

var el = $("<div/>");
el.html(text).children().remove();
return el.text();
like image 1
Ivan Lychkov Avatar answered Oct 13 '22 04:10

Ivan Lychkov