473,387 Members | 1,650 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.

Appending an HTML string to a DOM element.

Daz
Hi everyone!

Is it possible to take a line of text like so:

<tr><td>title1</td><td>title2</td><td>title3</td><td>title4</td></tr>

And append it to a DOM node such as this:

var nodeToAppendTo = document.getElementById('tbody');

I have tried using innerHTML, but I think I am misusing it, as it only
seems to append the text, and not the HTML elements.

Many thanks.

Daz.

Dec 19 '06 #1
7 9447
Daz wrote:
Hi everyone!

Is it possible to take a line of text like so:

<tr><td>title1</td><td>title2</td><td>title3</td><td>title4</td></tr>

And append it to a DOM node such as this:

var nodeToAppendTo = document.getElementById('tbody');

I have tried using innerHTML, but I think I am misusing it, as it only
seems to append the text, and not the HTML elements.

Many thanks.

Daz.
I added a division tag (just to make sure you actually have one) and I
added <table></tabletags to your "line of text" because the stuff you
inject into your page still has to be valid html which means if you're
going to insert table elements you need to have the table declaration.
<div id='tbody'></div>

<script src='text/javascript'>

var someString =
'<table><tr><td>title1</td><td>title2</td><td>title3</td><td>title4</td></tr></table>';
var nodeToAppendTo = document.getElementById('tbody');
nodeToAppendTo.innerHTML = someString;

// or just

document.getElementById('tbody').innerHTML = someString;
</script>
Hope that helps you out a bit.

--
http://www.hunlock.com -- Musings in Javascript, CSS.
$FA
Dec 19 '06 #2

Daz wrote:
Hi everyone!

Is it possible to take a line of text like so:

<tr><td>title1</td><td>title2</td><td>title3</td><td>title4</td></tr>

And append it to a DOM node such as this:

var nodeToAppendTo = document.getElementById('tbody');
Possible, yes, but you will not get correct results in all browsers.
IE in particular does not like you modifying tables using innerHTML.

<URL:
http://msdn.microsoft.com/workshop/a.../innerhtml.asp
>
Use DOM methods. An alternative, if you know you are adding table row
elements, is to add HTML to the string to make it a full table, add the
HTML to a temporary element using innerHTML, then extract the bits you
want.

Untested code:

function addTableRowsByInnerHTML (id, rowHTML)
{
var tableHTML = '<table>' + rowHTML + '</table>';
var tempEl = document.createElement('div');
document.body.appendChild(tempEl);
tempEl.innerHTML = tableHTML;
var newRows = tempEl.getElementsByTagName('tr');
var table = document.body.appendChild(id);

for (var i=0, len=newRows.length; i<len; i++){
table.appendChild(newRows[i]);
}

document.body.removeChild(tempEl);
}

However, DOM methods are better. Consider using the widely supported
DOM insertRow and insertCell methods.

insertRow:
<URL: http://www.w3.org/TR/DOM-Level-2-HTM...ml#ID-39872903 >

insertCell:
<URL: http://www.w3.org/TR/DOM-Level-2-HTM...ml#ID-68927016 >
I have tried using innerHTML, but I think I am misusing it, as it only
seems to append the text, and not the HTML elements.
Yes, you're missusing it.

--
Rob

Dec 19 '06 #3
Daz

RobG wrote:
Daz wrote:
Hi everyone!

Is it possible to take a line of text like so:

<tr><td>title1</td><td>title2</td><td>title3</td><td>title4</td></tr>

And append it to a DOM node such as this:

var nodeToAppendTo = document.getElementById('tbody');

Possible, yes, but you will not get correct results in all browsers.
IE in particular does not like you modifying tables using innerHTML.

<URL:
http://msdn.microsoft.com/workshop/a.../innerhtml.asp

Use DOM methods. An alternative, if you know you are adding table row
elements, is to add HTML to the string to make it a full table, add the
HTML to a temporary element using innerHTML, then extract the bits you
want.

Untested code:

function addTableRowsByInnerHTML (id, rowHTML)
{
var tableHTML = '<table>' + rowHTML + '</table>';
var tempEl = document.createElement('div');
document.body.appendChild(tempEl);
tempEl.innerHTML = tableHTML;
var newRows = tempEl.getElementsByTagName('tr');
var table = document.body.appendChild(id);

for (var i=0, len=newRows.length; i<len; i++){
table.appendChild(newRows[i]);
}

document.body.removeChild(tempEl);
}

However, DOM methods are better. Consider using the widely supported
DOM insertRow and insertCell methods.

insertRow:
<URL: http://www.w3.org/TR/DOM-Level-2-HTM...ml#ID-39872903 >

insertCell:
<URL: http://www.w3.org/TR/DOM-Level-2-HTM...ml#ID-68927016 >
I have tried using innerHTML, but I think I am misusing it, as it only
seems to append the text, and not the HTML elements.

Yes, you're missusing it.

--
Rob
Hi Rob.

I am developing an XPI for firefox, so cross-browser compatibility
shouldn't be an issue. Perhaps I should have mentioned that, but at the
time I felt that it was off-topic, and of no relevance to my query.

I can't seem to get my head around why using innerHTML will work for a
<divbut not for a <tbody>. Why does using a temporary element make a
difference? Do <divelements have some kind of magical power over
other elements. I have to admit, that I generally don't use <div>
elements as much as I should, as I tend to do most of the formatting
using tables, and never really felt that <divelements would benefit
me at all.

Thanks for your reply.

Daz.

Dec 19 '06 #4
Daz wrote:
RobG wrote:
Daz wrote:
Hi everyone!
>
Is it possible to take a line of text like so:
>
<tr><td>title1</td><td>title2</td><td>title3</td><td>title4</td></tr>
>
And append it to a DOM node such as this:
>
var nodeToAppendTo = document.getElementById('tbody');
Possible, yes, but you will not get correct results in all browsers.
IE in particular does not like you modifying tables using innerHTML.
[...]
I am developing an XPI for firefox, so cross-browser compatibility
shouldn't be an issue. Perhaps I should have mentioned that, but at the
time I felt that it was off-topic, and of no relevance to my query.
Play with the following:

<button onclick="
document.getElementById('xx').innerHTML =
'<tr><td>new cell 0<td><td>new cell 1';
">Replace using innerHTML</button>
<button onclick="
document.getElementById('xx').innerHTML +=
'<tr><td>new cell 0<td><td>new cell 1';
">Append using innerHTML</button>
<table id="xx">
<tr><td>cell 0<td>cell 1
</table>

I can't seem to get my head around why using innerHTML will work for a
<divbut not for a <tbody>.
There is nothing special about a div with regard to innerHTML, however
IE has issues with innerHTML and tables if you replace only part of the
table.
Why does using a temporary element make a
difference?
That was suggested to allow a table to be added using innerHTML in way
that works in IE.
Do <divelements have some kind of magical power over
other elements. I have to admit, that I generally don't use <div>
elements as much as I should, as I tend to do most of the formatting
using tables, and never really felt that <divelements would benefit
me at all.
Then you should learn more about using CSS, divs and spans for layout
rather than tables. Some will pursue the topic with religious fervour,
it's more appropriate to discuss it in
comp.infosystems.www.authoring.html or stylesheets.

An example:

<URL:
http://tjkdesign.com/articles/one_ht...ss_layouts.asp >

--
Rob

Dec 19 '06 #5
Daz

RobG wrote:
Daz wrote:
RobG wrote:
Daz wrote:
Hi everyone!

Is it possible to take a line of text like so:

<tr><td>title1</td><td>title2</td><td>title3</td><td>title4</td></tr>

And append it to a DOM node such as this:

var nodeToAppendTo = document.getElementById('tbody');
>
Possible, yes, but you will not get correct results in all browsers.
IE in particular does not like you modifying tables using innerHTML.
[...]
I am developing an XPI for firefox, so cross-browser compatibility
shouldn't be an issue. Perhaps I should have mentioned that, but at the
time I felt that it was off-topic, and of no relevance to my query.

Play with the following:

<button onclick="
document.getElementById('xx').innerHTML =
'<tr><td>new cell 0<td><td>new cell 1';
">Replace using innerHTML</button>
<button onclick="
document.getElementById('xx').innerHTML +=
'<tr><td>new cell 0<td><td>new cell 1';
">Append using innerHTML</button>
<table id="xx">
<tr><td>cell 0<td>cell 1
</table>

I can't seem to get my head around why using innerHTML will work for a
<divbut not for a <tbody>.

There is nothing special about a div with regard to innerHTML, however
IE has issues with innerHTML and tables if you replace only part of the
table.
Why does using a temporary element make a
difference?

That was suggested to allow a table to be added using innerHTML in way
that works in IE.
Do <divelements have some kind of magical power over
other elements. I have to admit, that I generally don't use <div>
elements as much as I should, as I tend to do most of the formatting
using tables, and never really felt that <divelements would benefit
me at all.

Then you should learn more about using CSS, divs and spans for layout
rather than tables. Some will pursue the topic with religious fervour,
it's more appropriate to discuss it in
comp.infosystems.www.authoring.html or stylesheets.

An example:

<URL:
http://tjkdesign.com/articles/one_ht...ss_layouts.asp >

--
Rob
Thanks for that Rob, you're an absolute star! That tiny piece of code
has made me realise so much more about the DOM than I did before.

I am not bashing the way your code is written, however, I was under the
impression that all tags needed to be closed. Can you advise on whether
it's actually a bad habit, or whether actually it's acceptable to do? I
think it could save a substantial amount of code being sent to the
browser, but I have never seen it before (apart from in my own code
when I forget to close a tag).
Would you actually write code like that for a website, or is it
something you do when your playing with code?
Are there any browsers that wouldn't be able to interpret that code
properly? (Apart from ancient browsers).
Is that how javascript would append elements to the DOM? (Or would it
do that on some browsers, but not all).

All the best, and many thanks for your assistance. :)

Daz.

Dec 21 '06 #6
Daz

RobG wrote:
Daz wrote:
RobG wrote:
Daz wrote:
Hi everyone!

Is it possible to take a line of text like so:

<tr><td>title1</td><td>title2</td><td>title3</td><td>title4</td></tr>

And append it to a DOM node such as this:

var nodeToAppendTo = document.getElementById('tbody');
>
Possible, yes, but you will not get correct results in all browsers.
IE in particular does not like you modifying tables using innerHTML.
[...]
I am developing an XPI for firefox, so cross-browser compatibility
shouldn't be an issue. Perhaps I should have mentioned that, but at the
time I felt that it was off-topic, and of no relevance to my query.

Play with the following:

<button onclick="
document.getElementById('xx').innerHTML =
'<tr><td>new cell 0<td><td>new cell 1';
">Replace using innerHTML</button>
<button onclick="
document.getElementById('xx').innerHTML +=
'<tr><td>new cell 0<td><td>new cell 1';
">Append using innerHTML</button>
<table id="xx">
<tr><td>cell 0<td>cell 1
</table>

I can't seem to get my head around why using innerHTML will work for a
<divbut not for a <tbody>.

There is nothing special about a div with regard to innerHTML, however
IE has issues with innerHTML and tables if you replace only part of the
table.
Why does using a temporary element make a
difference?

That was suggested to allow a table to be added using innerHTML in way
that works in IE.
Do <divelements have some kind of magical power over
other elements. I have to admit, that I generally don't use <div>
elements as much as I should, as I tend to do most of the formatting
using tables, and never really felt that <divelements would benefit
me at all.

Then you should learn more about using CSS, divs and spans for layout
rather than tables. Some will pursue the topic with religious fervour,
it's more appropriate to discuss it in
comp.infosystems.www.authoring.html or stylesheets.

An example:

<URL:
http://tjkdesign.com/articles/one_ht...ss_layouts.asp >

--
Rob
Hello again. I have also discovered where I was going wrong. After
comparing your cide to mine, I could still see no reason why it wasn't
working. Here's a snippet from my code:

function test()
{
var testAnimal = new aacres_animal("43234");
var table = document.createElement('table');
var tbody = document.createElement('tbody');
tbody.innerHTML = '<tr>'
+'<td>Animal Name</td>'
+'<td>Animal Type</td>'
+'<td>Discipline</td>'
+'<td>Discipline<br />Level</td>'
+'<td>Competitions<br />Entered</td>'
+'</tr>';
table.appendChild(tbody);
table.border = '1';
var newwin = window.open("","", 'height=400, width=400,
titlebar=yes, toolbar=yes');
newwin.document.body.appendChild(table);
tbody.appendChild(testAnimal.mainTR);
}

Now for some reason, this code was not working. After having a play
around with it, I discovered that by appending the tbody to the table,
and THEN using innerHTML to append my 'string of code' to the tbody...
It worked!

So here's the resulting code:

function test()
{
var testAnimal = new aacres_animal("43234");
var table = document.createElement('table');
var tbody = document.createElement('tbody');
table.appendChild(tbody);
tbody.innerHTML = '<tr>'
+'<td>Animal Name</td>'
+'<td>Animal Type</td>'
+'<td>Discipline</td>'
+'<td>Discipline<br />Level</td>'
+'<td>Competitions<br />Entered</td>'
+'</tr>';
table.border = '1';
var newwin = window.open("","", 'height=400, width=400,
titlebar=yes, toolbar=yes');
newwin.document.body.appendChild(table);
tbody.appendChild(testAnimal.mainTR);
}

Note that the only thing different is the point at which I append the
tbody, which for some reason has to be 'after' I have appended the
tbody to the table.

Thanks again for all of your help. :)

Daz.

Dec 21 '06 #7
Daz wrote:
RobG wrote:
[...]
Play with the following:

<button onclick="
document.getElementById('xx').innerHTML =
'<tr><td>new cell 0<td><td>new cell 1';
">Replace using innerHTML</button>
<button onclick="
document.getElementById('xx').innerHTML +=
'<tr><td>new cell 0<td><td>new cell 1';
">Append using innerHTML</button>
<table id="xx">
<tr><td>cell 0<td>cell 1
</table>
[...]
I am not bashing the way your code is written, however, I was under the
impression that all tags needed to be closed. Can you advise on whether
it's actually a bad habit, or whether actually it's acceptable to do?
Closing tags for td and tr elements are optional.

<URL: http://www.w3.org/TR/html4/struct/tables.html#edef-TD >
<URL: http://www.w3.org/TR/html4/struct/tables.html#edef-TR >

I
think it could save a substantial amount of code being sent to the
browser, but I have never seen it before (apart from in my own code
when I forget to close a tag).
Would you actually write code like that for a website, or is it
something you do when your playing with code?
Just for play, I think it is better to close tags for real work.

Are there any browsers that wouldn't be able to interpret that code
properly? (Apart from ancient browsers).
Any HTML 4 compliant browser should be OK with it. If not, it's a
browser bug.
Is that how javascript would append elements to the DOM? (Or would it
do that on some browsers, but not all).
The DOM is independent of the markup. HTML is just a language that
tells the browser how to build a DOM. Once the DOM is built, the
browser has no further use for the source markup. When converting from
DOM to HTML (say when getting the innerHTML of an element) the browser
may or may not decide to include optional tags - though most seem to as
it is probably simpler to include them rather than decide which ones to
omit.
--
Rob

Dec 21 '06 #8

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

Similar topics

1
by: Johnny | last post by:
I have below a sample parent-child xml that I'm trying to manipulate <Persons> <Person Id='1'/> </Persons> I will have a string that I want to append to my existing xml shown below:
1
by: Jonathan Taylor | last post by:
I have a large XML file, that is too large to read in to XmlDocument. I need to append data to this XML file without creating a new file, since I don't want to have two copies of the large file...
4
by: Jesper Stocholm | last post by:
I have a database class that maintains data about customers i my system. The basic XML for this looks like: <Chunk> <Vendor> <Database/> </Vendor> </Chunk> When a user is to be registrered...
1
by: Ron Adam | last post by:
In my program I have a lot of statements that append elements, but sometimes I don't want to append the element so it requres an if statement to check it, and that requires assigning the returned...
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...
3
by: Jim | last post by:
Could anyone please point me towards some code that allows me to add to an existing XML file using the output of an HTML form. I want to add a form on my website so users can input their email...
2
by: psbasha | last post by:
Hi Is it possible to Concatenate/Append list of elements into a single string as shown below ? I/P: list = O/P:
1
by: ofuuzo1 | last post by:
Hi, Is there anyway I can append a new element to an existing xml without first loading the existing file into a variable, adding the new element into the variable and saving it by overwriting the...
1
by: irixdude | last post by:
I am trying to create a script to enter the values of an array into a dynamically generated table 3 columns wide. I have a counter for the row # that I am using to name/id the row TR node so I an...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.