Click to See Complete Forum and Search --> : Confused


David Harrison
07-01-2003, 08:33 AM
I have just read a post about innerHTML, well I went to reply and typed out my message but the thread got closed in the mean time, so I'll have to ask in another thread.

I have this in the head:
document.getElementById("string").innerHTML=disp;

and this in the body:
<div id="string"></div>

now apparently this isn't standard, so what should I actually use in this case, because I need clarification (yes, I'm that dense).

Charles
07-01-2003, 08:42 AM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<title>Example</title>
<script type="text/javascript">
<!--
onload = function () {document.getElementById('string').replaceChild(document.createTextNode('disp;'), document.getElementById('string').firstChild)}
// -->
</script>
<div id="string">&amp;nbsp;</div>

David Harrison
07-01-2003, 03:56 PM
I'm afraid I can't do that because the thing that writes to the div is already in a function that is run onload and then again and again and again because it self runs itself with a setTimeOut.
Is there another way?

Charles
07-01-2003, 04:00 PM
Use just the document.getElementById('string').replaceChild(document.createTextNode('disp;'), document.getElementById('string').firstChild) part but make sure that you call after the div exists.

David Harrison
07-01-2003, 04:08 PM
I tried what you said but it didn't seem to work, I've uploaded my code (it's not very long), so could you please take a look and point out what I'm doing wrong.

Charles
07-01-2003, 04:27 PM
To where have you uploaded this code?

David Harrison
07-01-2003, 04:31 PM
I often do this, it's just a little embarressing.

Charles
07-01-2003, 04:50 PM
It's working but you are drawing black on black. And note that "document.createTextNode()" creates a text node. If you want to insert an element then you will have to use "document.createElement()" and then go about inserting a text node. And note also that the H elements are for headings and not for sizes.

David Harrison
07-01-2003, 04:56 PM
Yeah I uploaded a slightly older version that still used headers (I've since mover onto div's) but I'm not writing black on black because I give each header a slightly different shade of yellow. It should be yellow on black.
Here's the working version.

Charles
07-01-2003, 05:10 PM
On your other version, change one line to read div#string{height:20px;text-align:center; color:#f00} and you will understand what's going on.

David Harrison
07-01-2003, 05:16 PM
I also changed another line (guess which one) to this:
document.getElementById('string').replaceChild(document.createTextNode(disp), document.getElementById('string').firstChild);
and the consequences were dire. it literally put <h6>Put</h6><h5>etc... on the screen, how can I get it to interpret the header (now div) tags as HTML rather than just text.

Edit: I got to go now, I'll probably be able to get back on at 11:05 am actual GMT time tomorrow (this forums seems to be 1 hr slow), that's break time for me at school so I'll have a whole 20 mins.

Charles
07-01-2003, 05:18 PM
Originally posted by lavalamp
I also changed another line (guess which one) to this:
document.getElementById('string').replaceChild(document.createTextNode(disp), document.getElementById('string').firstChild);
and the consequences were dire. it literally put <h6>Put</h6><h5>etc... on the screen, how can I get it to interpret the header (now div) tags as HTML rather than just text. As I mentioned above, "If you want to insert an element then you will have to use "document.createElement()" and then go about inserting a text node."

Khalid Ali
07-01-2003, 05:19 PM
Originally posted by lavalamp

document.getElementById("string").innerHTML=disp;

and this in the body:
<div id="string"></div>

now apparently this isn't standard,

only the innerHTML part of your code is not standard,and I won't be surprised if soon we see innerHTML as part of the standards(as it has happened in the past)

The problem with createText node will be that it will force the browser to read the code as text on the other hand innerHTML will automatically render html tags as they should be.So happy coding lavalamp..

David Harrison
07-01-2003, 05:23 PM
I've read what you've both written but I won't be able to digest it until tomorrow when I've read it again. By the way the reason I don't want to use string.innerHTML is beacuse it doesn't work in Netscape. However the general opinion is moving more and more towards, stuff Netscape. So I'm afraid it's unhappy coding from me for now. Why can't there just be one browser???:confused:
Thanks for now, bye.

Khalid Ali
07-01-2003, 05:26 PM
Originally posted by lavalamp
By the way the reason I don't want to use string.innerHTML is beacuse it doesn't work in Netscape.

If you are referring to <5 NS then its true that element.innerHTML will not work,however,it will work in all NS6+ browsers

David Harrison
07-01-2003, 05:27 PM
I tried it in NS 7.02 with no luck. So that's a bit strange.

Charles
07-01-2003, 05:27 PM
As I mentioned in some other thread that was censored by some Jack booted forum moderator, "innerHTML" is way much easier to use. However, you don't often have much excuse to go about changing elements and you ought not to be doing so here. It would be nice if styles weren't read-only in the W3C specification, but I think that changing classes is ok.