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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:07:35+00:00 2026-06-12T15:07:35+00:00

I am trying to parse this html through jQuery to get data1, data2, data3.

  • 0

I am trying to parse this html through jQuery to get data1, data2, data3. While I do get data2 and data3 I am unable to get data3 with my approach. I am fairly new to jQuery so please pardon my ignorance.

<html>
<body>
   <div class="class0">
    <h4>data1</h4>
    <p class="class1">data2</p>
    <div id="mydivid"><p>data3</p></div>    
   </div>
</body>
</html>

Here is how I am calling this in my jquery.

var datahtml = "<html><body><div class=\"class0\"><h4>data1</h4><p class=\"class1\">data2</p><div id=\"mydivid\"><p>data3</p></div></div></body></html>";

alert($(datahtml).find(".class0").text()); // Doesn't Work

alert($(datahtml).find(".class1").text()); // work 

alert($(datahtml).find("#mydivid").text()); // work

Only alert($(datahtml).find(".class0").text()); is not working the rest are working as expected. I am wondering it may be because class0 has multiple tag inside it or what?? How to get data1 in such scenario?

  • 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-06-12T15:07:37+00:00Added an answer on June 12, 2026 at 3:07 pm

    Its behaviour is weird as it igonores the html and body tag and start from first div with class = “class0”. The html is parsed as DOM elements but not added to DOM. For elements added to DOM the selector does not ignore body tag and apply selectors on document. You need to add the html to DOM as given below.

    Live Demo

    $('#div1').append($(datahtml)); //Add in DOM before applying jquery methods.
    
    alert($('#div1').find(".class0").text()); // Now it Works too
    
    alert($('#div1').find(".class1").text()); // work   
    
    alert($('#div1').find("#mydivid").text()); // work
    

    If we wrap your html within some html element to make it starting point instead of your first div with class=”class0″ then your selector will work as expected.

    Live Demo

    var datahtml = "<html><body><div><div class=\"class0\"><h4>data1</h4><p class=\"class1\">data2</p><div id=\"mydivid\"><p>data3</p></div></div></div></body></html>";
    
    alert($(datahtml).find(".class0").text()); // Now it Works too
    
    alert($(datahtml).find(".class1").text()); // work   
    
    alert($(datahtml).find("#mydivid").text()); // work
    

    What jQuery docs say about the jQuery parsing function jQuery() i.e. $()

    When passing in complex HTML, some browsers may not generate a DOM
    that exactly replicates the HTML source provided. As mentioned, jQuery
    uses 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.

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

Sidebar

Related Questions

I'm trying to get my jQuery to work in this flow: Parse json using
I am trying to use preg_replace function to parse this table in a Html
I'm trying to parse a html doc using some code I found from this
I'm trying to parse this XML I want to get a list of all
I am trying to parse an HTML string in Internet Explorer using jQuery, based
I'm trying to parse some HTML using XPath in Java. Consider this HTML: <td
I am trying to use JQuery to parse a sitemap.xml to look like this
I'm trying to parse this XML string: <?xml version=1.0 encoding=UTF-8 standalone=no?> <response type=success> <lots>
I'm trying to parse this JSOn with multiple entries of node tag: { nodes:
I am beginner in PHP. I am trying to parse this xml file. <relationship>

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.