Abe Miessler Abe Miessler - 1 year ago 122
Javascript Question

jQuery selector for an element that directly contains text?

I was able to get this partially working using the

selector, but my problem is if an element contains an element that contains the text it is still returned. For example:


Will select both divs below:

<div>something else

fiddle: http://jsfiddle.net/TT7dR/

How can I select only divs that "directly" contain the text? Meaning that in the above example only the child div would be selected.


Just to clarify, if I were searching for the text "something else" instead of "test" then I would like to only find the parent div.

Answer Source

$('div>:contains("test")') is not a general solution, it only works for your specific example. It still matches any element whose descendants contain the text test, as long as its parent is a div.

There is in fact currently no selector that will select only direct parents of text nodes containing your target text. To do it you would have to walk the DOM tree yourself checking each text node you find for the target text, or write a plugin to do the same. It'd be slow, but then not as slow as :contains already is (it's not a standard CSS selector so you don't get browser-native fast selector support).

Here's a plain DOM function you could use as a starting point. It might be improved to find text in adjacent (non-normalised) text nodes, or to hide it in a plugin/selector-extension.

function findElementsDirectlyContainingText(ancestor, text) {
    var elements= [];
    return elements;

    function walk(element) {
        var n= element.childNodes.length;
        for (var i= 0; i<n; i++) {
            var child= element.childNodes[i];
            if (child.nodeType===3 && child.data.indexOf(text)!==-1) {
        for (var i= 0; i<n; i++) {
            var child= element.childNodes[i];
            if (child.nodeType===1)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download