20

I have a mild preference in solving this in pure JS, but if the jQuery version is simpler, then jQuery is fine too. Effectively the situation is like this

<span id="thisone">
 The info I want
 <span id="notthisone">
  I don't want any of this nonsense
 </span>
</span>

I effectively want to get
The info I want
but not
The info I want I don't want any of this nonsense
and I especially don't want
The info I want <span id="notthisone"> I don't want any of this nonsense </span>
which is unfortunately what I am getting right now...

How would I do this?

tshepang
  • 12,111
  • 21
  • 91
  • 136
Mala
  • 14,178
  • 25
  • 88
  • 119

6 Answers6

34

With js only:

Try it out: http://jsfiddle.net/g4tRn/

var result = document.getElementById('thisone').firstChild.nodeValue;    

​alert(result);​

With jQuery:

Try it out: http://jsfiddle.net/g4tRn/1

var result = $('#thisone').contents().first().text();  

alert(result);​

Bonus:

If there are other text nodes in the outer <span> that you want to get, you could do something like this:

Try it out: http://jsfiddle.net/g4tRn/4

var nodes = document.getElementById('thisone').childNodes;
var result = '';

for(var i = 0; i < nodes.length; i++) {
    if(nodes[i].nodeType == 3) {       // If it is a text node,
        result += nodes[i].nodeValue;  //    add its text to the result
    }
}

alert(result);
​
user113716
  • 318,772
  • 63
  • 451
  • 440
  • @richardtallent - You mean like a hybrid? That's true. A little less code that way. http://jsfiddle.net/g4tRn/2/ – user113716 Jul 03 '10 at 17:11
3

If you just want the first child then it's rather simple. If you are looking for the first text-only element then this code will need some modification.

var text = document.getElementById('thisone').firstChild.nodeValue;
alert(text);
Jason McCreary
  • 71,546
  • 23
  • 135
  • 174
1

Have you tried something like this?

var thisone = $("#thisone").clone();
thisone.children().remove();
var mytext = thisone.html();
richardtallent
  • 34,724
  • 14
  • 83
  • 123
0

Pure JavaScript

In this pure JavaScript example, I account for the possibility of multiple text nodes that could be interleaved with other kinds of nodes. Pass a containing NodeList in from calling / client code.

function getText (nodeList, target)
{
    var trueTarget = target - 1;
    var length = nodeList.length; // Because you may have many child nodes.

    for (var i = 0; i < length; i++) {
        if ((nodeList[i].nodeType === Node.TEXT_NODE) && (i === trueTarget)) {
            return nodeList.childNodes[i].nodeValue;
        }
    }

    return null;
}

You might use this function to create a wrapper function that uses this one to accumulate multiple text values.

Anthony Rutledge
  • 6,980
  • 2
  • 39
  • 44
0

To get a string of the child text nodes and not the element or other child nodes from a given element:

function getTextNodesText(el) {
  return Array.from(el.childNodes)
    .filter((child) => child.nodeType === Node.TEXT_NODE)
    .map((child) => child.textContent)
    .join("");
}
ggorlen
  • 44,755
  • 7
  • 76
  • 106
David Wolf
  • 1,400
  • 1
  • 9
  • 18
  • Dupe of https://stackoverflow.com/a/72970334/6243352. Please post the answer in one thread only. – ggorlen Sep 12 '22 at 17:47
0

FROM: http://viralpatel.net/blogs/2011/02/jquery-get-text-element-without-child-element.html

 $("#foo")
        .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
John Magnolia
  • 16,769
  • 36
  • 159
  • 270