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

innerHTML giving me some bother

Hi,

I have the following simple HTML page. I am trying to get the innerHTML
of the table element, "xmltable". I do intend to change the innerHTML
of this table but at this stage I am having simple problems.

When I create the following page, note that 'onload' the loadfunction()
function is run. In this function all that is happening is that it is
chacking that the browser supports getElementById and then display an
alert with the contents of element "xmltable":

<html>
<head>
<script>
<!--

function loadfunction() {
if (document.getElementById) {
alert(document.getElementById("xmltable").innerHTM L);
}
}

-->
</script>
</head>
<body onload="loadfunction();">
<table width="300" id="xmltable">
<tr>
<td width="100">First Name</td>
<td width="100">Surname</td>
<td width="100">Age</td>
</tr>
</table>
</body>
</html>

The alert, however, is slightly different that what is actually
contained within "xmltable". I am getting the following:

<TBODY>
<TR>
<TD width=100>First Name</TD>
<TD width=100>Surname</TD>
<TD width=100>Age</TD>
</TR><TBODY>

Apart from the fact that the tags are in caps, what is the TBODY tags
as they are not there. How do I just get the actual string value of the
contents within "xmltable" and not the above. And more importantly, how
do I change the contents to insert a different row markup because ...

document.getElementById("xmltable").innerHTML = newHTML;

.... doesnt work.

Cheers

Burnsy

Aug 17 '05 #1
4 1742
bi******@yahoo.co.uk wrote:
Hi,

I have the following simple HTML page. I am trying to get the innerHTML
of the table element, "xmltable". I do intend to change the innerHTML
of this table but at this stage I am having simple problems.

When I create the following page, note that 'onload' the loadfunction()
function is run. In this function all that is happening is that it is
chacking that the browser supports getElementById and then display an
alert with the contents of element "xmltable":

<html>
<head>
<script>
The type attribute is required:

<script type="text/javascript">
<!--
HTML comments inside script tags are unnecessary and potentially harmful.

function loadfunction() {
if (document.getElementById) {
alert(document.getElementById("xmltable").innerHTM L);
}
}

-->
</script>
</head>
<body onload="loadfunction();">
<table width="300" id="xmltable">
<tr>
<td width="100">First Name</td>
<td width="100">Surname</td>
<td width="100">Age</td>
</tr>
</table>
</body>
</html>

The alert, however, is slightly different that what is actually
contained within "xmltable". I am getting the following:

<TBODY>
<TR>
<TD width=100>First Name</TD>
<TD width=100>Surname</TD>
<TD width=100>Age</TD>
</TR><TBODY>
I guess there are <table> tags outside the body tags.

Apart from the fact that the tags are in caps, what is the TBODY tags
as they are not there.
The tbody tags are optional, and aren't in your source HTML, however
tbody elements are mandatory in a table, so the browser inserts them.

Table elements can only contain, CAPTION, COL or COLGROUP, THEAD, TFOOT
or TBODY elements.
How do I just get the actual string value of the
contents within "xmltable" and not the above. And more importantly, how
do I change the contents to insert a different row markup because ...
By 'actual string value' I guess you want the source. The simple answer
is you can't, and more importantly you should not use innerHTML to
modify a table, use document object model (DOM) methods.

innerHTML is not an open or public standard, it is a Microsoft
proprietary method. It has been widely copied by other browsers and is
very convenient for some uses. However, MS expressly state that you
should not use it to "change the contents of the table, tFoot, tHead,
and tr elements" though you can use it to modify the contents of a td or
to write an entire table.

<URL:http://msdn.microsoft.com/workshop/author/dhtml/reference/properties/innerhtml.asp>

document.getElementById("xmltable").innerHTML = newHTML;

... doesnt work.
It does 'work', just not how you expected it to. There is no public
standard that defines what it should do, so whatever it does is 'working'.

Cheers

Burnsy

--
Rob
Aug 17 '05 #2
> HTML comments inside script tags are unnecessary and potentially harmful.

Correct me if I'm wrong, but isnt this recommended or is it just old
practise where it was common for browsers to not recognise the <script>
tag?
By 'actual string value' I guess you want the source. The simple answer
is you can't, and more importantly you should not use innerHTML to
modify a table, use document object model (DOM) methods.


Ok, if I have a table with a sepcific ID, is it possible to alter the
rows (<tr><td></td></tr>)? How would this be done using DOM? Ideally if
you could provide a short example, it would be much appreciated.

Ultimately what I am trying to achieve is to dynamically ceate a table
on the 'client-side' with values taken from an XML file. Below is an
example of this file:

<?xml version="1.0" encoding="iso-8859-1"?>
<friends>
<person>
<firstname>Martin</firstname>
<surname>Bishop</surname>
<age>24</age>
</person>
<person>
<firstname>Robert</firstname>
<surname>Kennedy</surname>
<age>18</age>
</person>
<person>
<firstname>Nigel</firstname>
<surname>MacInnes</surname>
<age>23</age>
</person>
</friends>
The reason I would like to create this using JavaScript is because I
would like to be able to manipulate the table (ie. sort by column
header) without having to reload the page, where I would normally use
server-side scripting. The whole idea is to reduce page reloads. Is my
above method not capable of doing this? If so, if there a better way I
should be doing this? Ideally, I would like to use XML as it stores
values very neatly for this type of operation but if this isnt possible
I would be willing to explore other alternatives.

Cheers

Burnsy

Aug 17 '05 #3
bi******@yahoo.co.uk writes:
HTML comments inside script tags are unnecessary and potentially harmful.


Correct me if I'm wrong, but isnt this recommended or is it just old
practise where it was common for browsers to not recognise the <script>
tag?


It's just old practise from when browsers didn't understand the script
tag. Netscape 2 was the first browser to introduce script tags, and
pretty much all browsers made since then (March 1996) has understood
the script tag.

The "potentially harmful" comes from what the HTML/XML comment would
do if written in an XHTML document (allow the parser to remove the
content of the script element during parsing, i.e., goodbye
script). It's not necessary, and it's a bad habit to have in the
future (or now, if you write XHTML now and send it correctly to a
browser that understands it correctly).

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Aug 18 '05 #4
> Table elements can only contain, CAPTION, COL or COLGROUP, THEAD, TFOOT
or TBODY elements.


Is there any way to get something like this working then:
(html source)

<table width="200" id="tbl">
<tr>
<td width="100">old text</td><td width="100">new text</td>
</tr>
</table>
The by using the following statement:
document.getElementById("tbl").innerHtml = "<TBODY>\n<TR>\n<TD
width=100>Hello</TD>\n<TD width=100>Alright</TD>\n<TD
width=100>Goodbye</TD></TR></TBODY>";
....I could reset the row structure.

I have tried it, and failed, but could somethiing simply like this be
done? Im guessing that the innerHTML value isnt a simple string. Is it
possible to remake the new innerHTML string as nodes for insertion (or
am I a million miles away from how this could be done)?

Cheers

Burnsy

Aug 19 '05 #5

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

Similar topics

2
by: Martin Turner | last post by:
Can anyone tell me why the following bit of code doesn't work in Netscape (6) but does in IE6 ? It is just an example which is supposed to toggle some text in both the <textarea> and <p> elements...
4
by: Anders Nielsen | last post by:
Hi :-) I'm currently working with innerHTML , but it is giving me some problems with " and '. Basically (there is also some ASP involved), my problem looks like this: I would like to,...
162
by: techievasant | last post by:
hello everyone, Iam vasant from India.. I have a test+interview on C /C++ in the coming month so plz help me by giving some resources of FAQS, interview questions, tracky questions, multiple...
2
by: Sakharam Phapale | last post by:
Hi All, I am working on a project, where maximum operations carried out on Files and multi-dimensional arrays. Since array data is huge application takes too much memory. My problem is, after...
63
by: Jake Barnes | last post by:
In the course of my research I stumbled upon this article by Alex Russel and Tim Scarfe: http://www.developer-x.com/content/innerhtml/default.html The case is made that innerHTML should never...
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...
0
by: John | last post by:
Hi, I have an ASP.NET intranet application running on Customer A side. Customer B wants to access that application across the firewall. Customer A is giving some restrictions to access the...
7
by: John | last post by:
Hi Everyone, I'm having this extremely annoying problem with Internet Explorer 6, giving me an error message saying "unknown runtime error" whenever I try to alter the contents of a <divelement...
5
by: p vs np | last post by:
Hi. I have been trying to fiddle around with some layout managers, the CardLayout in particular. The idea I had was : I must be able to accept multiple panels on one frame, i.e, i must have the...
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
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.