It may be very simple, but I'm 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 the contents of html tags (from a string in javascript) and display that content on my html page in the right sections.
i.e. I want "This is <div id="title1">
be displayed in <div id="title1">
and "This is paragraph 1". to display in <div id="new1">
and for the second pair of tags.
I need all this to work only on the client side. I tried using the HTML DOM getElementByTagName method and its too complicated. I know very little about jquery. And I'm embarrassed. I don’t understand how to do this. Can you advise me what to use - javascript or jquery and how to use it? Is there a way to identify a string and scroll it?
How to extract “This is header1” (and similar content enclosed in html tags) from str1 ?? I do not know that the index of them therefore cannot use the substr () or substring () function in javascript.
source share