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

  • Home
  • SEARCH
  • 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 6144331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:38:05+00:00 2026-05-23T18:38:05+00:00

I want to use jQuery to select every element that has a certain text

  • 0

I want to use jQuery to select every element that has a certain text string in it, but not the parents of that element. How do I do this? I have no control whatsoever over the HTML code, but here is an example:

<body>
<div>
<p>This is a paragraph and <a>this is not</a>.</p>
Here we have a div.
</div>
</body>

If I use the word “this” as my match word, I want jQuery to provide me with a set containing the <a> and the <p>, but not the <div> or the <body>.

Again, I have no control AT ALL over the HTML!

Thanks!

** Clarification: I do want the parent of the element if the parent ALSO has a “this” in its immediate text. Thus, I want the <a> AND the <p>.

  • 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-23T18:38:05+00:00Added an answer on May 23, 2026 at 6:38 pm

    Update::

    Here is what I came up with: jsfiddle

    var myArray = $('*:contains("this")','body').filter(function(){
        if($(this).contents().filter(function(){
            return(this.nodeType == 3);
        }).text().indexOf('this')===-1){
            return false;
        }
        return true;
    });
    
    
    $.each(myArray,function(){
       console.log(this.nodeName); 
    });
    

    Starts similar to the link posted by Robin, but it forces to only search in the context of body elements – this keeps your scripts safe if they are not inline.

    The next part is a filter that checks to see if the current element direct text nodes contain the text.

    This is a bit convoluted, but to walk through it:

    .contents() – docs – gets the immediate nodes

    .filter() – docs – we want to only test on test nodes, so we filter them out

    this.nodeType – w3 spec – check to see if its a text node

    .test() – docs – gets a string of the text nodes.

    .indexOf() – check that string for our string

    Note I did the :contains() at the top and in the second filter, the first isn’t needed per say but I think the initial test should reduce the number of deeper tests and speed it up slightly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I already started this in javascript so I don't want to use jquery but
I want to use jquery live() to automatically select the text in a text
Hi i want to get changed text value from JQuery but i can't select
I want to use jquery draggable/droppable to let the user select a group of
I want to use jQuery with a GridView which contains textboxes, but I'm stuck
I don't want to use jQuery, but I'd like to use Ajax to do
So, I want to use jquery to set the text of a button, depending
In an HTML table I want to select some items, I use jQuery in
Ok, this is a weird request, but I need to use jQuery to manipulate
First: I'm not really good at javascript and I want to use jQuery functions.

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.