Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6719935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:09:06+00:00 2026-05-26T09:09:06+00:00

I was able to get this partially working using the :contains selector, but my

  • 0

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:

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

Will select both divs below:

<div>something else
   <div>test</div>
</div>

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.

UPDATE:

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T09:09:07+00:00Added an answer on May 26, 2026 at 9:09 am

    $('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= [];
        walk(ancestor);
        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) {
                    elements.push(element);
                    break;
                }
            }
            for (var i= 0; i<n; i++) {
                var child= element.childNodes[i];
                if (child.nodeType===1)
                    walk(child);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was able to get this working is SSRS 2008, but do to the
In code: //I know that to get this effect (being able to use it
Thanks to the answer on this stackoverflow question I was able to get the
This is a puzzle for me, I am able to get three DropDownLists to
I've had several clients ask this, and I've never been able to get or
I need to be able get a single specific attribute from an element with
Has anyone been able to get a variable record length text file (CSV) into
I'm able to get cells to format as Dates, but I've been unable to
There are a few posts about this, but after hours of searching I still
I have some code (which I cannot change) that I need to get working

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.