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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T04:03:03+00:00 2026-05-21T04:03:03+00:00

I just discovered this issue, and I wanted to run it by everyone here

  • 0

I just discovered this issue, and I wanted to run it by everyone here to make sure I wasn’t missing obvious something before I reported it.

Here is what’s causing me a problem:

html = '<html><body><div id="test">This is just a test</div></body></html>';
alert( $(html).find('#test').html() );

The alert window shows null instead of the text inside #test. HOWEVER, if I simply wrap <div id="test"> in another div element, it works properly and returns the expected, “This is just a test”.

This code works:

html = '<html><body><div><div id="test">This is just a test</div></div></body></html>';
alert( $(html).find('#test').html() );

Can someone explain to me why this would be happening? Why would the second example work but the first one not?

  • 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-21T04:03:04+00:00Added an answer on May 21, 2026 at 4:03 am

    If you debug what $(html) does, you will notice it only creates the DOM tree for the “usefull” contents, here the div#test block (it strips the html and body containers).
    Thing is, find() looks for children of the element you’re calling it from. As the root element of $(html) is already your div#test element, you will not be able to search for itself using find.

    This also explains why wrapping a “container” element makes your code work.

    EDIT
    A simple workaround would be, as the OP said, to warp the html into a container such as a div:

    var content = $('<div/>').append(html);
    content.find('#test'); // returns the right div
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just discovered a bug where the code looked something like this: char *foo
I just discovered this operator and I was wondering if it is safe to
I just discovered this component and started working with it. I understand that the
Possible Duplicate: Python or Ruby Interpreter on iOS I just discovered this apps pypad
I am working with zend just recently. I discovered this ViewScript decorator for form
[edit]: I'm terribly sorry for my timing on asking this question. I've just discovered
Just discovered an issue with the sorting plugin used on our cart, was working
I just discovered forge and wanted to try it out at once. I closely
Just ran into an issue with a has_many :through association and after/before-destroy callbacks not
Ok this is probably just me not knowing enough about php but here it

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.