473,387 Members | 3,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Convert HTML string to element

I want to use element.appendChild(child) on a string of HTML I have:

html=

'<div>
<strong>Text</strong>
</div>'

However, since it's not an element (ie created with
document.createElement) I can't use appendChild with my html string.
The only way I know to get it in the document is to use
element.innerHTML, however I want to use appendChild. So my question
is, how can I convert my HTML string into an element object that I can
use with appendChild()?

Mar 12 '07 #1
1 10640
On Mar 12, 4:52 pm, "egg...@gmail.com" <egg...@gmail.comwrote:
I want to use element.appendChild(child) on a string of HTML I have:

html=

'<div>
<strong>Text</strong>
</div>'

However, since it's not an element (ie created with
document.createElement) I can't use appendChild with my html string.
The only way I know to get it in the document is to use
element.innerHTML, however I want to use appendChild. So my question
is, how can I convert my HTML string into an element object that I can
use with appendChild()?
Use createElement('div') and insert the string using innerHTML. Then
cycle through the div's child nodes and insert them where you wish.

<script type="text/javascript">

function insertHTML (el, htmlString) {
var p = el.parentNode;
var d = document.createElement('div');
d.innerHTML = htmlString;
for (var i=d.childNodes.length; i; i) {
p.insertBefore(d.childNodes[--i], el.nextSibling);
}
}

</script>
<button onclick="
var s = '<p>Here is a paragraph with <span style=\'color:red;\'>'
+ 'some red <b>text</b></span></p>'
+ '<p>Another paragraph</p>';
insertHTML(document.getElementById('xx'), s);
">Insert stuff</button>
<div id="xx"></div>

If you intend inserting into tables, you'll need something a bit
smarter.
--
Rob
Mar 12 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Fu Bo Xia | last post by:
anyone know a Java (or Java usable) package that converts XML Schema (XSD) documents into a tree form? thanks, fu bo
1
by: Jens Mueller | last post by:
Hi there, this is a Java-XML Question, so I am not sure whether this is the right place, haven't found anything better .... I try to convert a Java object to XML via SAX and let the FOP...
2
by: Brian | last post by:
I used the designer in VS.NET 2003 to create a dataset. Within that dataset, there is an element "Tags", with a primary key and relationship: <xs:element name="Tags"> <xs:complexType>...
5
by: jk | last post by:
I'm having trouble converting a datatable into xml, with resonse.write to aspx. I'm basically converting vb code that saved a recordset into a stream into c#, but the format is wrong. I've tried...
4
by: aevans1108 | last post by:
expanding this message to microsoft.public.dotnet.xml Greetings Please direct me to the right group if this is an inappropriate place to post this question. Thanks. I want to format a...
25
by: Charles Law | last post by:
I thought this was going to be straight forward, given the wealth of conversion functions in .NET, but it is proving more convoluted than imagined. Given the following <code> Dim ba(1) As...
3
by: Joe | last post by:
Hi I have a dataset with 2 tables and Relations What is the best way to flatten the 2 files to a new table or xml or file I can loop thru table1 and get the childrows or I can do an Xpath on...
9
by: Phil_Harvey | last post by:
I am redoing my website and trying to get it to do something more exciting using Javascript. I did normal Java at university and code at work in VB.NET. I have got reasonably far into what I want...
7
by: samelzoro | last post by:
here is a problem in recursion: unexpected result ? by this program I just want to convert xml dom's document object to xml-string. (for all browsers) //load a xml function...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.