jQuery selector for an element that directly contains text?

I was able to get this partially working using the :contains 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.



$('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)

Just to complete the knowledge base. If you need to get all DOM elements within the body (not only DIVs) that contain specific text or characters you can use:

function getNodesThatContain(text) {
    var textNodes = $(document).find(":not(iframe, script)")
          function() {
           return this.nodeType == 3 
             && this.textContent.indexOf(text) > -1;
    return textNodes.parent();


Hope that helps.

jsfiddle: http://jsfiddle.net/85qEh/2/

Credits: DMoses


try adding the greater than:


This seems to work for me:

$('div >:contains("test")');


This forces the matched :contains selector to be a direct child of the <div> element


Try the following:


Finds specific element, but not parents

var elementsContainingText = ($(':contains("' + text + '")', target)).filter(function() {
    return $(this).contents().filter(function() {return this.nodeType === 3 && this.nodeValue.indexOf(text) !== -1; }).length > 0;

Add more alternative:

if($(selector).text().trim().length) {
   var thetext = $(selector).contents().filter(function(){
     return this.nodeType === 3;                     



It will select the text only and remove any element with tag!



You can simply select the element that doesn't have your element

$('div:contains("test"):not(:has(> div))')

less code to write (but with a little limitation):

let selector = $('div:contains("test")');

Just use the jQuery function (.has) because the css :has is experimental: https://developer.mozilla.org/en-US/docs/Web/CSS/:has#Browser_compatibility


When you have a structure like this:


Then only the inner div - Element will be found by this solution. This is because there is still an Element - Child of the div that :contains the string "test".


You might have to do an in-efficient query. Do not use this solution if someone finds a selector that manages to filter out child elements: http://viralpatel.net/blogs/2011/02/jquery-get-text-element-without-child-element.html

    .clone()    //clone the element
    .children() //select all the children
    .remove()   //remove all the children
    .end()  //again go back to selected element

edit: that snippet above is just to test the element, in implementation it would look more like this: http://jsfiddle.net/rkw79/TT7dR/6/

$("div:contains('test')").filter(function() {
    return (
    $(this).clone() //clone the element
    .children() //select all the children
    .remove() //remove all the children
    .end() //again go back to selected element
    .filter(":contains('test')").length > 0)
}).css('border', 'solid 1px black');


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.