www.webdeveloper.com
Results 1 to 3 of 3

Thread: creating a table using innerhtml

  1. #1
    Join Date
    Jun 2010
    Posts
    62

    creating a table using innerhtml

    I want to use innerhtml to create a table

    I thought if I modified line:

    document.getElementById("p1").innerHTML="New text!";

    replace the word New Text with

    <table width="200" border="1">
    <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
    </table>

    It would work, but it doesn't

    here's some sample code I want to modify:



    <html>
    <head>
    <script type="text/javascript">
    function ChangeText()
    {
    document.getElementById("p1").innerHTML="New text!";
    }
    </script>
    </head>

    <body>
    <p id="p1">Hello world!</p>
    <input type="button" onclick="ChangeText()" value="Change text" />
    </body>
    </html>

  2. #2
    Join Date
    Sep 2009
    Posts
    93

    Thumbs up

    Hey it works,

    Note:- in javascript statments must be in a line,

    change your code as below

    <html>
    <head>
    <script type="text/javascript">
    function ChangeText()
    {
    document.getElementById("p1").innerHTML="<table width='200' border='1'><tr><td>&nbsp;</td><td>&nbsp;</td></tr></table>";
    }
    </script>
    </head>

    <body>
    <p id="p1">Hello world!</p>
    <input type="button" onclick="ChangeText()" value="Change text" />
    </body>
    </html>

  3. #3
    Join Date
    May 2009
    Posts
    29
    Try this

    Code:
    function ChangeText()
    {
    var newTEXT = document.getElementById('p1').innerHTML = newTEXT;
                         document.getElementById("p1").innerHTML="New text!"; 
    }

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