innerHTML in Netscape

M

Martin Turner

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'>
var on = false;
function fill_text() {
if(!on) {
document.getElementById('test1').innerHTML="Testing";
document.getElementById('test3').innerHTML="Hello";
on= true;
}
else {
document.getElementById('test1').innerHTML="";
document.getElementById('test3').innerHTML="";
on= false;
}
</script>
<body>
<textarea name='test1' id='test1'></textarea>
<p name='test3' id='test3'></p>
<button name='test2' id='test2' onclick='fill_text();'>Do It</button>
</body>
</html>
----End problem code------------

Thanks for any help
Martin.
 
C

Csaba2000

I have verified this in NN 6.1 (after putting in the missing } in fill_text()),
and I don't have an explanation for you although I can tell you from
past experience that NN is not a happy camper when modifying
innerHTML for some elements such as <BUTTON>.

if you use .value instead of .innerHTML for the textarea,
you get the expected behaviour.

Csaba Gabor from New York
 
L

Lasse Reichstein Nielsen

Martin Turner said:
Can anyone tell me why the following bit of code doesn't work in Netscape
(6) but does in IE6 ?

Netscape 6 is a badly broken piece of beta software, and should be
upgraded ASAP. However, the problem occurs in later Mozillas too.
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.

In Mozilla Firebird 0.6, the same problem occurs. The most probable
reason is that using innerHTML on a textarea isn't the best way of
writing to it. The contents of a textarea *isn't* HTML, it is plain
text (so inner*HTML* isn't appropriate), and to change it, you use the
value property.
document.getElementById('test1').innerHTML="Testing";

Try:
document.getElementById('test1').value = "Testing";

If you check it, using "innerHTML" actually inserts a text node as a
child of the textarea. The contents of that node is visible through
the textarea. Try pressing "Do It", and then change the contents of the
textarea manually.
on= false;
}

Missing a "}" here in the example.

Short summary: don't use innerHTML on elements that cannot contain
HTML.

/L
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top