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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:40:52+00:00 2026-05-24T23:40:52+00:00

If I run this code – var html= ‘<html><head></head><body><div class=bar></div></body></html>’; console.log($(html).find(‘div’)); I get no

  • 0

If I run this code –

var html= '<html><head></head><body><div class="bar"></div></body></html>';
console.log($(html).find('div'));

I get no results returned, if I run this code –

var html= '<html><head></head><body><div><div class="bar"></div></div></body></html>';
console.log($(html).find('div'));

Then I get a single result returned – the inner div (<div class="bar"></div>). I would have expected the first code snippet to return a single result and the second snippet two results.

Similarly, this code returns no results –

var code = $("<div id='foo'>1</div><div id='bar'>2</div>");
console.log(code.find('div'));

but this code alerts ‘div’ twice –

var code = $("<div id='foo'>1</div><div id='bar'>2</div>");
code.each(function() {
    alert( this.nodeName );
})

Given the result of the second snippet, I would have expected the first code snippet to return two results. Could someone please explain why I’m getting the results I’m getting?

http://jsfiddle.net/ipr101/GTCuv/

  • 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-24T23:40:53+00:00Added an answer on May 24, 2026 at 11:40 pm

    Let’s split this question into two parts.

    First:

    var html= '<html><head></head><body><div class="bar"></div></body></html>';
    console.log($(html).find('div'));
    

    and

    var html= '<html><head></head><body><div><div class="bar"></div></div></body></html>';
    console.log($(html).find('div'));
    

    do not work because according to the jQuery() docs:

    When passing in complex HTML, some browsers may not generate a DOM
    that exactly replicates the HTML source provided. As mentioned, we use
    the browser’s .innerHTML property to parse the passed HTML and insert
    it into the current document. During this process, some browsers
    filter out certain elements such as <html>, <title>, or <head>
    elements. As a result, the elements inserted may not be representative
    of the original string passed.

    • In the first code block, your <html>, <head>, and <body> tags are getting stripped out, and <div class="bar"></div> remains. find only searches inside the resulting <div>, and cannot find anything.
    • In the second code block, your <html>, <head>, and <body> tags are getting stripped out, and <div><div class="bar"></div></div> remains. find searches inside the result, and finds a single <div>.

    As for your second part:

    var code = $("<div id='foo'>1</div><div id='bar'>2</div>");
    console.log(code.find('div'));
    

    You first give jQuery a string, which it takes and makes into a jQuery object with the two <div>‘s. Then, find searches in each <div>, finds nothing and returns no results.

    Next, in

    var code = $("<div id='foo'>1</div><div id='bar'>2</div>");
    code.each(function() {
        alert( this.nodeName );
    })
    

    each loops through the jQuery object, taking each of the two created <div>‘s, and alerts their node name. Therefore, you get two alerts.

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

Sidebar

Related Questions

If I run this code, var waitRow = $(this).parent().parent().next().get(0); $(waitRow).children('td:nth-child(2)').html('some text').toggle(); toggle is not
this is my code : <div id=test style=width:200px;height:100px;background:red class=tabs change_font_size> <div> <a class=delete style=float:right;font-size:20px;text-decoration:underline;cursor:pointer;>delete</a>
When I run this code var button=document.getElementById(button) if (navigator.userAgent.indexOf('iPhone') != -1) { button.style.left =
I am trying to run this code $files = array('readme.txt', 'test.html', 'image.gif'); $zip =
When I run this code the selected item is not visible. I've already tried
when i run this code : CONADefinitions.CONAPI_FOLDER_INFO2 FolderInfo; int iResult = 0; IntPtr Buffer
I have a question concerning this code which I want to run on QNX:
I receive this Run-Time Check Failure upon the return in the following code. I
Why run this code and print whole string? #include <stdio.h> void main() { int
Why this code don't work,when i want run this code vwd 2008 express show

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.