473,383 Members | 1,862 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,383 software developers and data experts.

innerHTML and tables...

I know you aren't supposed to use innerHTML to mess with table
structure, but it seems you can't use it just after a table without
damaging the containing element.

I added a table to a div using createElement methods, then added a bit
of extra text using innerHTML, only to find most of the attributes
removed from the table.

Below is a script that calls the same code to add a table inside a
div. It adds an onclick to the div and change the style. When it is
called without an extra bit of innerHTML, all is fine. But if
innerHTML is also used, the background-color is removed as is the onclick.

I know there's no spec for innerHTML, but the above behaviour happens
on IE & Firefox Windows as well as Firefox & Safari Mac.

It doesn't seem to happen if a div is used instead of a table. Is
this a known effect of innerHTML?

<script type="text/javascript">

function addDiv ( el, b ) {
var oT = document.createElement( 'table' );
var oTb = document.createElement( 'tbody' );
var oTr = document.createElement( 'tr' );
var oTd = document.createElement( 'td' );
oTd.appendChild( document.createTextNode( 'text added to table' ) );
oTd.style.backgroundColor = '#ddddff';
oTr.appendChild( oTd );
oTb.appendChild( oTr );
oT.appendChild( oTb );
oT.onclick = sayHi;
el.appendChild( oT );
if ( b ) el.innerHTML += 'Here\'s some more text';
}

function sayHi(){
alert( 'Hi from ' + this.nodeName );
}

</script>

<div onclick="addDiv( this, false );">click me 1</div>
<div onclick="addDiv( this, true );">click me 2</div>


--
Rob
Jul 26 '05 #1
4 6525
VK


RobG wrote:
I know you aren't supposed to use innerHTML to mess with table
structure, but it seems you can't use it just after a table without
damaging the containing element.

I added a table to a div using createElement methods, then added a bit
of extra text using innerHTML, only to find most of the attributes
removed from the table.


I guess that the removed attributes are JavaScript functions and
listeners.

Change you "extra text" block to this:

if (b) {
var txt = document.createElement('SPAN');
txt.innerHTML = 'Here\'s some more text';
el.appendChild(txt);
}

and it will work again. Somewhere there was a link about JavaScript
inserted as a text stream. It doesn't get parsed in such case.
And your instruction DOMNode+text transforms it into text stream. Just
a wild guess but seems to be true.

Jul 26 '05 #2
RobG wrote:
oT.onclick = sayHi;
if ( b ) el.innerHTML += 'Here\'s some more text';


When you do that el.innerHTML += ...
you are not (just) adding to el, you are completely redefining it.
That is, el.innerHTML += ... means:
el.innerHTML = el.innerHTML + ...
and if some property is not sitting in the innerHTML, when you redefine
it (that is, a property of el or any of its sub elements), by means of
setting its innerHTML), it's gone.

To that end, it may be instructive to actually see what that innerHTML
is. Redefine your oT.onclick like so:

oT.onclick = function(el) {return function() {
alert('Hi from ' + this.nodeName + "\n" + el.innerHTML); } } (el);

Good luck,
Csaba Gabor from Vienna

Jul 26 '05 #3
VK wrote:
and it will work again. Somewhere there was a link about JavaScript
inserted as a text stream. It doesn't get parsed in such case.


On IE 6, the following will be injected and parsed. However, IE will
then claim that: Internet Explorer cannot open the Internet site ...
Operation aborted (ed: dummy, you've already loaded it) and unload it
in front of your eyes.

<body>
<script type='text/javascript'>
function scriptInjector() {
myscript = document.createElement('script');
myscript.type = 'text/javascript';
document.documentElement.appendChild(myscript);
myscript.text =
"alert('script has been parsed injected and parsed');"
}
scriptInjector();
</script>
</body>
However, take out the scriptInjector() call and change the <body> to
<body onload="scriptInjector()"> as below, and the script works just
fine for me on my Win XP Pro system:

<body onload="scriptInjector()">
<script type='text/javascript'>
function scriptInjector() {
myscript = document.createElement('script');
myscript.type = 'text/javascript';
document.documentElement.appendChild(myscript);
myscript.text =
"alert('script has been parsed injected and parsed');"
}
</script>
</body>
With Firefox, version Deer Park Alpha 1 both versions work without
error.
Go figure,
Csaba Gabor from Vienna

Jul 26 '05 #4
Csaba Gabor wrote:
RobG wrote:
oT.onclick = sayHi;
if ( b ) el.innerHTML += 'Here\'s some more text';

When you do that el.innerHTML += ...
you are not (just) adding to el, you are completely redefining it.
That is, el.innerHTML += ... means:
el.innerHTML = el.innerHTML + ...
and if some property is not sitting in the innerHTML, when you redefine
it (that is, a property of el or any of its sub elements), by means of
setting its innerHTML), it's gone.

To that end, it may be instructive to actually see what that innerHTML
is. Redefine your oT.onclick like so:

oT.onclick = function(el) {return function() {
alert('Hi from ' + this.nodeName + "\n" + el.innerHTML); } } (el);


It appears to be that when you add an onclick event to any element using
DOM, it is not added to its innerHTML. When you modify the innerHTML of
the element's parent, the element is written out again, without the
onclick attribute (this seems to be consistent for any intrinsic event)
effectively removing it.

While I originally thought it only happened with tables, with further
testing it appears that the same thing happens with other elements too -
intrinsic events are not included in the innerHTML if they aren't in the
original HTML source and have been added using some other method.

It does not seem to happen with other element attributes - style, id,
name, etc. nor does it happen if you modify the innerHTML of the element
itself, nor if you add the event using innerHTML (I guess that's
obvious, but...).

It's just a gotcha with innerHTML that I was not aware of before.

I can only guess that dynamically attached events are not included in an
elements' innerHTML because they may be added using a number of methods
and that some may be anonymous functions. But in that case I would have
thought that the function body would become the onclick attribute:

script: oT.onclick = function() { alert('hi'); };

==> innerHTML: ... onclick="alert('hi');" ...
and so on. Is this why the W3C has not added an innerHTML-type method
to the DOM?

--
Rob
Jul 27 '05 #5

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

Similar topics

8
by: Kyle | last post by:
I am presently making use of documentElement.innerHTML to retrieve page contents for manipulation, but I've noticed that the sting value returned is not identical to the actual page source....
22
by: necromonger | last post by:
Hi, I've got this code that creates a new new row and cell. I then put some text into the cell with innerHTML - works beautifully with Firefox but fails with IE. I guess IE doesn't support this...
6
by: wk | last post by:
i have a <table></table> ...and the have a string with the html that will create the table rows, columsn etc....perhaps <tr><td>a row</td></tr>. this string comes dynamically from a source and...
1
by: Andrew Phillipo | last post by:
I have some code that works everywhere but IE5.0, including IE5.5. Here is a snippet of where the code seems to go wrong: Location.prototype.change = function(current) { this.current = current;...
9
by: Hallvard B Furuseth | last post by:
Why does the FAQ (Q 4.15) recommend innerHTML when so many here say one should use createElement(), replaceChild() etc? Also, why does the "Alternative DynWrite function" at...
2
by: sveinn | last post by:
Hi all, I've read through this group searching for an answear about this problem. Few have come close but not quite what I need. My problem is this: I'm using Ajax to fetch a new table with...
3
by: ilia | last post by:
Hi All, I am a newbie in terms of Javascript, I found some code on the net to swap rows in a table using innerHTML, this works fine in Firefox but IE is complaining, after some googling around I...
9
by: martymix | last post by:
simple question: I have a simple <dt>test text</dt> I get the innerHTML of that dt, and I try and append some text to it like so: dt = document.getElementsByTagName('dt') var text =...
6
by: David Gillen | last post by:
Hello, I'm hoping someone can shed some light on this, problem is in IE6 and 7. FF is okay. I have within my html <tbody id="dataTable" name="dataTable"></tbody> And then some javascript...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...

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.