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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:03:00+00:00 2026-05-23T08:03:00+00:00

I have an on the fly (not part of the DOM document), HTML code.

  • 0

I have an on the fly (not part of the DOM document), HTML code. Something like-

<div class="media"> 
abc
</div>
<div class="media"> 
efg
</div>  
<div class="nvid">
   <div class="media">
qwr
   </div>    
</div>

now i make a jQuery obj for the above html using,

jq = jQuery(above html);

then i select the divs having class as ‘media’ by using the syntax-

jQuery('div.media', jq).each(console.log("found"));

now, ideally i should get three ‘found’ printed on the command line, but i am getting only one. any ideas, what i am missing?

  • 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-23T08:03:01+00:00Added an answer on May 23, 2026 at 8:03 am

    Edited for clarity.

    Behind the scenes using the core jQuery(selector, context) will perform a .find(). Reading the docs on .find() it states it will search the current elements’ children.

    Meaning your call will do the following:

    Take <div class="media">abc</div>‘s children — 0 children — and look for .media 0 results.

    Take <div class="media">efg</div>‘s children — 0 children — and look for .media 0 results.

    Take <div class="nvid">qwr</div>‘s children — 0 children — and look for .media 0 results.

    To do it the DOM way, wrap it in a div and do your find:

    var html = '<div class="media">abc</div>' + 
               '<div class="media">efg</div>' +
               '<div class="nvid">qwr</div>';
    var obj = $(html);
    //this fails:
    $('div.media',obj).each(function(){console.log("found with jquery object");});
    
    // this works as a single DOM object
    var wrap = $('<div/>').append(obj).eq(0);
    $('div.media',wrap).each(function(){console.log("found with DOM object");});
    

    The reason this works is because we insert your html as children, and do the search on the parent container (wrapper) and it gets 3 children, and it matches 2 of those children.

    To do it the filter() way and return a NEW jquery object with just those elements: link to filter docs

    obj.filter('.media').each(function(){console.log("found with Filter");});
    

    Based on your comment you can do something like:

    $(html).filter('.media').each(function(){
       var myDiv = $(this);
       // do stuff with myDiv.html();
    });
    

    You can checkout the jsfiddle here – slightly outdated now.

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

Sidebar

Related Questions

I have the following test-class made on the fly: http://sumoin.pastebin.com/ff744ea4 No fine-tuning or something
I'm not a very experience programmer and have been learning HTML/CSS/JS on the fly.
I have a PHP class that creates a PNG image on the fly and
I have a .NET assembly DLL that is created on-the-fly from pre-compiled code in
I have some PHP code that generates dynamic tables of data on the fly.
If I have a div that is a 5x5 square or something. I want
I have created a VB.NET 2008 Class Library that has, for the most part,
I have the need to read the Thunderbird address book on the fly. It
I have read a lot that LISP can redefine syntax on the fly, presumably
I have 3 PDF documents that are generated on the fly by a legacy

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.