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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:24:38+00:00 2026-05-29T10:24:38+00:00

Maybe this is very basic, but I am all confused. I have a simple

  • 0

Maybe this is very basic, but I am all confused.
I have a simple html page with many sections (div). I have a string containing html tags in javascript. The code is as follows:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript">
var str1="<html><body><div id='item1'><h2>This is a heading1</h2><p>This is a paragraph1.</p></div><div id='item2'><h2>This is a heading2</h2><p>This is another paragraph.</p></div><div id='lastdiv'>last</div></body></html>";
</script>
</head>
<body>
<div id="title1"></div>
<div id="new1"></div>
<div id="title2"></div>
<div id="new2"></div>
</body>
</html>

I want to extract content of the html tags (from the string in javascript) and display that content in my html page in desired sections.

i.e. I want “This is a heading1” displayed in <div id="title1"> and “This is a paragraph1.” to be displayed in <div id="new1"> and same for the second pair of tags.

I need all of this to work only on the client side. I have tried to use HTML DOM getElementByTagName method and its getting too complicated. I know very little of jquery. And I am confused. I dont understand how to go about it. Can you guide me what to use – javascript or jquery and how to use it? Is there a way to identify the from the string and iterate through it?

How to extract “This is heading1” (and similar contents enclosed in the html tags) from str1?? I don’t know the index of these hence cannot use substr() or substring() function in javascript.

  • 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-29T10:24:40+00:00Added an answer on May 29, 2026 at 10:24 am

    Using .text() as both a ‘getter’ and a ‘setter’ we can just repeat the pattern of:

    • target the element on the page we wish to fill
    • give it content from
      the string

    jsFiddle

    <script type="text/javascript">
    var str1="<html><body><div id='item1'><h2>This is a heading1</h2><p>This is a paragraph1.</p></div><div id='item2'><h2>This is a heading2</h2><p>This is another paragraph.</p></div><div id='lastdiv'>last</div></body></html>";
    $(function(){
        var $str1 = $(str1);//this turns your string into real html 
    
        //target something, fill it with something from the string
        $('#title1').text( $str1.find('h2').eq(0).text() );
        $('#new1').text( $str1.find('p').eq(1).text() );
        $('#title2').text( $str1.find('h2').eq(1).text() );
        $('#new2').text( $str1.find('p').eq(1).text() );
    })
    </script>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this maybe a very basic question but I'm having a bit of
This may be a very basic question, but it still gets me confused (and
Maybe this is something very easy to do it but so far it's taking
Maybe a very simple question. How can I put in this code <Query> <Where>
This may be very simple question,But please help me. i wanted to know what
This may be a very simple problem, but I couldn't find an answer googleing
this may be a very simple question, but is it possible to force a
Am not sure if this question makes sense. But I know all the basic
Ok, so this is maybe a very basic question about REST concepts or Rails
This may be a very noobie question, but in today's world of web app

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.