473,473 Members | 1,468 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

xhtml, innerHtml, appendChild, and innerHTML. what is the exact proper way to do this with DOM

Ok,
i am sure everyone is sick of hearing about this. but i've checked
about 10 different posts/sites about this issue, and they all say "use
DOM" but i think there is more to be said. Perhaps I am a total newbie
but the answer was not immediately obvious to me here.
so.. problem:
declaring doctype as xhtml will prevent myDiv.innerHtml=val from
working.

suggested solution:
use DOM.
var myNewDiv = document.createElement( 'div' );
myDiv.appendChild( myNewDiv );
now we have to set that new div's value somehow right? of course
myNewDiv.innerHtml will not work. so using DOM puts us in the same
exact spot. unless i went about this the wrong way.. i realize that
using dom to appen a input text will work because .value property will
be set no problem, but how do we set just a text value?

and how come
myDiv.innerHTML = val // all caps HTML
still works?

Jul 10 '06 #1
6 2974
sonic said the following on 7/10/2006 2:09 PM:
Ok,
i am sure everyone is sick of hearing about this. but i've checked
about 10 different posts/sites about this issue, and they all say "use
DOM" but i think there is more to be said. Perhaps I am a total newbie
but the answer was not immediately obvious to me here.
so.. problem:
declaring doctype as xhtml will prevent myDiv.innerHtml=val from
working.
Only in browsers that support xHTML (IE doesn't).
suggested solution:
use DOM.
var myNewDiv = document.createElement( 'div' );
myDiv.appendChild( myNewDiv );
Give your div an ID:

myDiv.id="test";

now we have to set that new div's value somehow right? of course
myNewDiv.innerHtml will not work. so using DOM puts us in the same
exact spot.
var el = document.getElementById("test");
//remove all childNodes
while (el.firstChild) {
el.removeChild(el.firstChild);
}
//insert new Text
el.appendChild(document.createTextNode(theTextToAd d));
}
unless i went about this the wrong way.. i realize that
using dom to appen a input text will work because .value property will
be set no problem, but how do we set just a text value?
See above.
and how come
myDiv.innerHTML = val // all caps HTML
still works?
In which browser?

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Temporarily at: http://members.aol.com/_ht_a/hikksnotathome/cljfaq/
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jul 10 '06 #2
thanks for quick response Randy.
1. the ID was not the issue. I already had a reference to my control. I
just didn't know about the document.createTextNode method. thank you
for that.

2. innerHTML works for me in firefox 1.5.0.4 and MSIE
6.0.2900.2180.xpsp_sp2_gdr.......
my doc declaration is:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
Randy Webb wrote:
sonic said the following on 7/10/2006 2:09 PM:
Ok,
i am sure everyone is sick of hearing about this. but i've checked
about 10 different posts/sites about this issue, and they all say "use
DOM" but i think there is more to be said. Perhaps I am a total newbie
but the answer was not immediately obvious to me here.
so.. problem:
declaring doctype as xhtml will prevent myDiv.innerHtml=val from
working.

Only in browsers that support xHTML (IE doesn't).
suggested solution:
use DOM.
var myNewDiv = document.createElement( 'div' );
myDiv.appendChild( myNewDiv );

Give your div an ID:

myDiv.id="test";

now we have to set that new div's value somehow right? of course
myNewDiv.innerHtml will not work. so using DOM puts us in the same
exact spot.

var el = document.getElementById("test");
//remove all childNodes
while (el.firstChild) {
el.removeChild(el.firstChild);
}
//insert new Text
el.appendChild(document.createTextNode(theTextToAd d));
}
unless i went about this the wrong way.. i realize that
using dom to appen a input text will work because .value property will
be set no problem, but how do we set just a text value?

See above.
and how come
myDiv.innerHTML = val // all caps HTML
still works?

In which browser?

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Temporarily at: http://members.aol.com/_ht_a/hikksnotathome/cljfaq/
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jul 10 '06 #3
sonic wrote:
thanks for quick response Randy.
What response? Why is top-posting becoming common here? Please
interleave replies with trimmed quotes from whatever you are replying
to. Now the conversation is completely out of sequence...

[...]
Randy Webb wrote:
sonic said the following on 7/10/2006 2:09 PM:
Ok,
i am sure everyone is sick of hearing about this. but i've checked
about 10 different posts/sites about this issue, and they all say "use
DOM" but i think there is more to be said. Perhaps I am a total newbie
but the answer was not immediately obvious to me here.
so.. problem:
declaring doctype as xhtml will prevent myDiv.innerHtml=val from
working.
It will fail using that capitalisation (perhaps you've not copied
actual code), but otherwise, why should it fail just because you are
using XHTML[1]? There is no public specification to say where, when or
how innerHTML should work, so test it widely before use.

The general idea is that innerHTML should only be used for simple
things - it is really handy to completely replace the content of an
element with a bit of text with minimal markup. It is also common to
use it with AJAX where large chunks of documents are returned for
insertion into a document, but I that doesn't make it good.

There are some big differences in implementations between browsers,
there is no published standard for it and it isn't (and likely never
will be) part of a W3C standard.

Only in browsers that support xHTML (IE doesn't).
Even using XHTML strict, Firefox allows the use of innerHTML without
complaint. I'd expect it to fail with XML, not XHTML. Afterall, XHTML
(1.0) is just HTML written as XML. 1.1 is somewhat different, but even
then innerHTML still works in Firefox (and IE).

[...]
and how come
myDiv.innerHTML = val // all caps HTML
still works?
In which browser?
IE and Firefox 1.5.
1. Whether it is 'better' to use XHTML rather than HTML has been argued
long and hard in many forums. The concensus is that unless you have a
really good reason to use XHTML and understand the ramifications, stick
with HTML. Otherwise, there are some significant down-sides and no
positives to XHTML.

--
Rob

Jul 11 '06 #4
sonic wrote:
<snip>
2. innerHTML works for me in firefox 1.5.0.4 and MSIE
6.0.2900.2180.xpsp_sp2_gdr.......
As IE does not support XHTML at all the odds are that you are actually
employing (more or less) formally malformed HTML (resulting in the
receiving browser creating an HTML DOM to be scripted) and so it is not
surprising that innerHTML works.
my doc declaration is:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
An XHTML DOCTYPE does not make a document into an XHTML document (even
if it could not be an XHTML document without one). For web browser the
critical factor is the HTTP content type header. If you serve a document
as text/html then it is interpreted as a (tag soup) HTML document. Any
resemblance that document may have to XHTML is irrelevant to the browser
(beyond forcing the browser to do a lot of extra work error-correcting
it back to (tag soup) HTML), though it may fool the author.

However, if you are going to script a document as an HTML document it is
perverse to mark it up as if it was not an HTML document.
Randy Webb wrote:
<snip>

Please do not top post to comp.lang.javascript.

Richard.
Jul 11 '06 #5
RobG said the following on 7/10/2006 10:45 PM:

<snip>
>>Only in browsers that support xHTML (IE doesn't).

Even using XHTML strict, Firefox allows the use of innerHTML without
complaint. I'd expect it to fail with XML, not XHTML. Afterall, XHTML
(1.0) is just HTML written as XML. 1.1 is somewhat different, but even
then innerHTML still works in Firefox (and IE).
Not in IE. IE doesn't support xHTML so it can't possibly support
innerHTML in xHTML. And it is even debatable whether FF is getting true
xHTML from the OP.
[...]
>>>and how come
myDiv.innerHTML = val // all caps HTML
still works?
In which browser?

IE and Firefox 1.5.
Firefox, yes. IE, no. See above.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Temporarily at: http://members.aol.com/_ht_a/hikksnotathome/cljfaq/
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jul 11 '06 #6
RobG wrote:
sonic wrote:
>>thanks for quick response Randy.


What response? Why is top-posting becoming common here?
Google Groups.

Consider the Usenet Improvement Project: http://blinkynet.net/comp/uip5.html
--
"The most convoluted explanation that fits all of the made-up facts is
the most likely to be believed by conspiracy theorists. Fitting the
actual facts is optional."
Jul 11 '06 #7

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

Similar topics

1
by: Nicolas Van Lancker | last post by:
Hi folks; I have this webpage, allowing users to insert multiple records in one post into the database. Because I don't know the exact number of records they want to add, I created a little...
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...
1
by: jhcorey | last post by:
I am doing some maintenance on a js tree control that was done with proprietary IE code. When I click on a node it calls a function which has code similar to that below to expand the tree. "el"...
10
by: oopaevah | last post by:
When using innerHTML to insert xhtml IE5+ mangles the xhtml completely. For example it removes the quotes from attributes. It also does other ridiculous things like on a <div> it will insert the...
4
by: RobG | last post by:
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...
10
by: webEater | last post by:
Hello, I try the following in Firefox and other modern browsers: window.addEventListener('load', function() { document.title = CSS.getClass('fontSize'); var div = document.createElement('div');...
0
by: Zhang Weiwu | last post by:
Hello. I am pretty sure this problem has been mentioned somewhere else but I wasn't able to google it out yet. I have two pages, in xhtml and in html, they do the same thing: create a table cell...
4
by: Dan Andrews | last post by:
Hello, I was wondering what is the correct way to handle special characters via javascript and the DOM. I would like to avoid document.write and innerHTML. What I am doing is dynamically...
4
by: Marijn | last post by:
Hey everybody, The following has been posted before (2 years ago) but no response was added. Therefor again, the following code for creating xhtml file: <?php error_reporting(6143); ...
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...
1
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...
1
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
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 ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.