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

I run this code for experimenting copy constructor and assignment operator class AClass {
When I try to run this code: import java.io.*; import java.util.*; public class TwoColor
After doing something I run this code: var notification = webkitNotifications.createNotification( 'icon.png', // icon
When I compile and run this code (it's part of a much larger program),
When i try to run this code i get the error: Warning: mysql_fetch_array() expects
If I run this code: svn_output = %x[svn update /Users/radek/Sites/db2.rft -r 11105 --force ]
When I run this code in iPhone simulator ,I can not delete record from
I am trying to run this code in android using eclips IDE. int maxrow=0;
I am trying to run this code where I have a list of lists.
When I run this code, the variable items only appends 9999 chars and rest

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.