innerHTML in Netscape

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Martin Turner

    innerHTML in Netscape

    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='JavaS cript'>
    var on = false;
    function fill_text() {
    if(!on) {
    document.getEle mentById('test1 ').innerHTML="T esting";
    document.getEle mentById('test3 ').innerHTML="H ello";
    on= true;
    }
    else {
    document.getEle mentById('test1 ').innerHTML="" ;
    document.getEle mentById('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_t ext();'>Do It</button>
    </body>
    </html>
    ----End problem code------------

    Thanks for any help
    Martin.



  • Csaba2000

    #2
    Re: innerHTML in Netscape

    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

    "Martin Turner" <martin.turner@ skynet.be> wrote in message news:3f4b9341$0 $288$ba620e4c@r eader0.news.sky net.be...[color=blue]
    > 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='JavaS cript'>
    > var on = false;
    > function fill_text() {
    > if(!on) {
    > document.getEle mentById('test1 ').innerHTML="T esting";
    > document.getEle mentById('test3 ').innerHTML="H ello";
    > on= true;
    > }
    > else {
    > document.getEle mentById('test1 ').innerHTML="" ;
    > document.getEle mentById('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_t ext();'>Do It</button>
    > </body>
    > </html>
    > ----End problem code------------
    >
    > Thanks for any help
    > Martin.
    >
    >
    >[/color]


    Comment

    • Lasse Reichstein Nielsen

      #3
      Re: innerHTML in Netscape

      "Martin Turner" <martin.turner@ skynet.be> writes:
      [color=blue]
      > Can anyone tell me why the following bit of code doesn't work in Netscape
      > (6) but does in IE6 ?[/color]

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

      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.
      [color=blue]
      > document.getEle mentById('test1 ').innerHTML="T esting";[/color]

      Try:
      document.getEle mentById('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.
      [color=blue]
      > on= false;
      > }[/color]

      Missing a "}" here in the example.

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

      /L
      --
      Lasse Reichstein Nielsen - lrn@hotpop.com
      Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
      'Faith without judgement merely degrades the spirit divine.'

      Comment

      Working...