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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T07:21:55+00:00 2026-05-21T07:21:55+00:00

I load up some HTML using $.get or $.ajax . In the debugger for

  • 0

I load up some HTML using $.get or $.ajax. In the debugger for both Chrome and Firefox i can see the html has been loaded properly:

> data

"<html><head><title></title></head><body>
<div id="topdiv" style="display:none;width:880px; height:600px;"></div>
</body</html>"

So it’s a string of HTML. Great. Next I try to use jQuery to parse it:

$doc = $(data);

> $doc

[<TextNode textContent="\n\n">, 
title, 
<TextNode textContent="\n\n\n">, 
div#topdiv, 
<TextNode textContent="\n \n">]

Whoah, what? Where did html, head, body go? OK, well anyway, all I care about is the div

$div = $data.find('div');

> $div

[]

Huh??

OK. The div is empty. Upon further experimentation, I realize that jQuery basically ignores any top-level tags. Then it doesn’t actually let you select any 2nd level tags, and so if there is a div nested inside the outer div, that WILL be selected. But… what??

Seems jQuery kind of chokes on those “TextNode” elements, that, curiously, it created itself (instead of html, body). So I wrote some code to get around this by looping through the elements, and extracting the non-text nodes directly:

function getNodes($doc) {
    var result = new Array();
    for (var i = 0; i < $doc.length; i++) {
        if ($doc[i].hasOwnProperty("tagName")) {
            result.push($doc[i]);
        }
    }
    return $(result);
}

Worked great! On Chrome.

Tried it in Firefox, and nothing works again.

Turns out that not every object in Firefox has a hasOwnProperty function. WHAT?? Ok. So rewrite to this:

typeof($tempHtml[i].tagName) !== 'undefined'

Finally, works in Firefox too.

Wow. Am I just being stupid? Why is so much post processing needed just to turn a string of HTML into a jQuery object? I feel like I must be missing something obvious. Is there a way to do this that does not involve such mayhem?

All I am trying to do is load up some HTML and turn it into a jQuery object. Yet it seems to be an increasingly baffling ordeal. Am I just doing something fundamentally wrong? Why is this so complicated?

  • 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-21T07:21:56+00:00Added an answer on May 21, 2026 at 7:21 am

    Read the jQuery documentation – it addresses this issue.

    When you have a HTML string that contains <html>, <head>, or <body> elements, and you do this:

    $(str)
    

    then those elements will be ignored. Only elements that can be put inside a DIV are valid and will be added to the resulting jQuery instance object.


    $div = $data.find('div');
    

    This will result in an empty jQuery object because find() searches the descendants of the current set of elements (which is one TITLE element and one DIV element in your case – and those two elements don’t have descendant DIV elements).


    In order to remove the Text Node objects from your jQuery object, I recommend this approach:

    $data.filter(function() { return this.nodeType === 1; });
    

    To get the DIV element, I recommend this approach:

    $('<div>').html(str).find('div');
    

    where str is your HTML string.


    Alternative approach:

    $(str).filter('div');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to load some content into a page using Ajax. The html
I'm trying to load some dynamic content after the user has logged in, using
I am using jQuery's $.get() to load some content to a div whose id=results
So I'm trying to load some returned html from an .aspx page but a
I'm trying to get some basic authentication/authorization with devise/cancan with Rails. Rather than using
I want to load some images into my application from the file system. There's
I want to load some data from mysql into my cocoa application view before
I have an application which tries to load some expected registry settings within its
I'm trying to call a function after I load some XML into Actionscript, and
In the NodeMouseClick event I'm trying to load some objects from the Node.Tag. Before

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.