473,789 Members | 2,422 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.getEl ementById) {
alert(document. getElementById( "xmltable").inn erHTML);
}
}

-->
</script>
</head>
<body onload="loadfun ction();">
<table width="300" id="xmltable">
<tr>
<td width="100">Fir st Name</td>
<td width="100">Sur name</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>Surna me</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.getEle mentById("xmlta ble").innerHTM L = newHTML;

.... doesnt work.

Cheers

Burnsy

Aug 17 '05 #1
4 1766
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.getEl ementById) {
alert(document. getElementById( "xmltable").inn erHTML);
}
}

-->
</script>
</head>
<body onload="loadfun ction();">
<table width="300" id="xmltable">
<tr>
<td width="100">Fir st Name</td>
<td width="100">Sur name</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>Surna me</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.getEle mentById("xmlta ble").innerHTM L = 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>Mart in</firstname>
<surname>Bishop </surname>
<age>24</age>
</person>
<person>
<firstname>Robe rt</firstname>
<surname>Kenned y</surname>
<age>18</age>
</person>
<person>
<firstname>Nige l</firstname>
<surname>MacInn es</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 "potentiall y 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/rasterTriangleD OM.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.getEle mentById("tbl") .innerHtml = "<TBODY>\n<TR>\ n<TD
width=100>Hello </TD>\n<TD width=100>Alrig ht</TD>\n<TD
width=100>Goodb ye</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
3554
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 but what happens in Netscape is that the <textarea> seems to get overwritten rather than written to whereas the <p> element is just written to or removed. -------Problem code----- <html> <script language='JavaScript'>
4
1819
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, dynamically, insert the following html into a table cell: <span id="soeskende5slet" style="position: absolute; right: 70px; cursor:hand; text-decoration:underline;" onClick="soeskendeSlet('5','Anders
162
14923
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 choice questions.etc.. I'll be indebted to everyone.. Thanks in advance.. regards vasant shetty Bangalore
2
1584
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 few operations, toolbar on MDI from doesn't catch "Click" event. But it catch all other events like mouse leave etc.
63
4805
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 be used. I'm wondering, If I wanted all the content of BODY as a string, how else could I get except through innerHTML?
9
8671
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 <http://www.jibbering.com/faq/faq_notes/alt_dynwrite.html> need such a lot of tests to find out if innerHTML assignment actually works, instead of just inserting <span id="strange name"></span> and checking if the document now contains an element with that ID?
0
937
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 application. They are.. 1. Customer B should access the application only through Port 80. No other port will be opened on Customer A side. 2. No firewall configuration or re-configuration will be done on Customer A side. 3. Connection should initiated...
7
38027
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 using innerHTML. Now, I've researched this problem on the web, and found many references to it, but none of them quite addressed my specific situation, and since my experience with JavaScript is limited, I was not able to adapt the solutions I...
5
2340
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 first panel with a navigation button labeled "Next", and on clicking it, i am expected to see another panel on the frame. In my sample code, i have three classes: CardLayoutSelf, Page0, Page1. The design is intended to work as follows : 1....
0
9663
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10404
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10195
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9979
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9016
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7525
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6765
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4090
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2906
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.