www.webdeveloper.com
Results 1 to 4 of 4

Thread: what is innerHTML

  1. #1
    Join Date
    Jun 2004
    Location
    sutton in ashfield
    Posts
    13

    what is innerHTML

    hi,
    im just learning javascript and i have just come across
    document.getElementById("").innerHTML.
    i just wondered what it was used for as i cant figure it out
    thanks!

  2. #2
    Join Date
    Jan 2005
    Location
    Los Angeles, CA
    Posts
    4,887
    It allows you to sort-of-speak edit the value inside of
    an HTML tag.
    Code:
    <textarea cols=1 rows=1 id="ta">
    hi
    </textarea>
    the innerHTML of the above code is hi
    But you could have also done this with ta.value

    But innerHTML also lets you do this to any tag.



    Code:
    <div id="myLayer">
    <b><font color=blue>wuz up</font></b>
    </div>
    the innerHTML of the above code is:
    <b><font color=blue>wuz up</font></b>

    you can actually change all the HTML displayed inside of there with innerHTML!

    here's some code you can play with:
    Code:
    <script>
    function ONLOAD(){
    setTimeout("changeME()",1000)
    }
    function changeME(){
    document.getElementById("myLayer").innerHTML="<h2>Hi, this text will shrink!h2</h2>"
    setTimeout("changeME2()",1000)
    }
    function changeME2(){
    document.getElementById("myLayer").innerHTML="<h3>Hi, this text will shrink!h3</h3>"
    setTimeout("changeME3()",1000)
    }
    function changeME3(){
    document.getElementById("myLayer").innerHTML="<h4>Hi, this text will shrink!h4</h4>"
    setTimeout("changeME4()",1000)
    }
    function changeME4(){
    document.getElementById("myLayer").innerHTML="<h5>Hi, this text will shrink!h5</h5>"
    setTimeout("changeME5()",1000)
    }
    function changeME5(){
    document.getElementById("myLayer").innerHTML="<h6>Hi, this text will shrink!h6</h6>"
    setTimeout("changeME6()",1000)
    }
    function changeME6(){
    document.getElementById("myLayer").innerHTML="Now for the textbox!"
    setTimeout("changeME7()",3000)
    }
    function changeME7(){
    document.getElementById("myLayer").innerHTML="<input type='text'>"
    }
    </script>
    <body onload="ONLOAD()">
    <div id="myLayer"><h1>Hi, this text will shrink!h1</h1></div>
    </body>

  3. #3
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    innerHTML is quite a useful method to read/change the child(s)' content of a parent(root) element. Despite that, even it is recognized by all modern browsers, it is not a W3C recomanded method.

    The similar DOM recomened methods are:

    - element.data (or root.firstChild.data)
    - element.nodeValue (or root.firstChild.nodeValue) - well, W3C says that nodeValue is a readonly method, but I have used sometimes as a replacement method as well
    - the create/clone/append/replace DOM methods:
    document.createElement()
    document.createTextNode()
    element.cloneNode()
    root.appendChild()
    root.insertBefore()
    root.replaceChild()
    Last edited by Kor; 03-02-2005 at 03:03 AM.

  4. #4
    Join Date
    Jun 2004
    Location
    sutton in ashfield
    Posts
    13
    There just seems so much to learn and take in at the moment but thats a big weight off my shoulders,its been giving me an headache trying to work it out.cant wait till i get to the MATH part that should be
    fun.thanks alot guys you been a great help.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles