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 6779789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:25:46+00:00 2026-05-26T16:25:46+00:00

I have a text string i’m trying to select the spans from using jQuery.

  • 0

I have a text string i’m trying to select the spans from using jQuery. I’d like to grab the spans w/o adding the element to the dom — if that’s possible?

After reading the jquery docs i was under the assumption that i could create a fragment by wrapping the string in a jquery selector tag, then using.find() to find the elements i want.

I have code that is similar to this but from the looks of the last line, it’s obvious that no spans are being selected; any help would be greatly appreciated:

// 'text' is normally generated automatically... 
// just made it an escaped string for example purposes.
var text ="<span id=\"blah1\">Y</span><br/><span id=\"blah2\">o</span><br/>";
var spans = $(text).find('span');
console.log(text); // => <span id="blah1">Y</span><br/><span id="blah2">o</span><br/>
console.log(spans.length); // => 0 

Thanks.

  • 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-26T16:25:47+00:00Added an answer on May 26, 2026 at 4:25 pm

    You want to use filter(), not find()

    var text ="<span id=\"blah1\">Y</span><br/><span id=\"blah2\">o</span><br/>";
    var spans = $(text).filter('span');
    console.log(spans.length);
    

    jsFiddle

    From the jQuery docs

    filter:

    The supplied selector is tested against each element; all elements
    matching the selector will be included in the result.

    find:

    the .find() method allows us to search through the descendants of
    these elements in the DOM tree and construct a new jQuery object from
    the matching elements.

    with your html fragment, there is no wrapper element, so there is no descendants, hence why find() does not work.

    You are basically doing:

    var elems = jQuery("<span id=\"blah1\">Y</span>").add("<br/>").add("<span id=\"blah2\">o</span>").add("<br/>");
    

    If you want find to work with find(), you need to wrap it in an element.

    var text ="<span id=\"blah1\">Y</span><br/><span id=\"blah2\">o</span><br/>"; 
    var spans = jQuery("<div></div>").append(text).find("span");
    console.log(spans.length);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a text string structured like this: = Some Heading (1) Some text
I have a method that looks like this: public static String escape(String text) {
I have a text string, for ex. 'A vehicle travels from A to B,
I have a text string that contain some references numbers like [3][2][4] or [1]
I have got a text string like this: test1test I want to check if
Suppose I have a body of text (string) like this: str = 'This is
I have a list of keywords I'm trying to find in a text string.
ok imagine that we have text like this String s=123 45678 91011122314 1516; now
I have a jQuery variable. It's a text string. I want to create a
I have a text string that looks like this var1=red&var2=green&var3=blue&var4=magenta How can manipulate this

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.