Michael
Michael

Reputation: 13614

Insert table to div element

I want to insert dynamically created table to the div element after button "click me!" pressed.

Here it the code:

<body>
    <button onclick="CreatTable()">click me!</button>
    <div id="ShowDataID"></div>
    <script type="text/javascript">
        var data = ['First Name', 'Last Name', 'Email'];

        function CreatTable() {

            var table = document.createElement('table');
            //some logic to fill table
            document.getElementById('ShowDataID').innerHTML = table;
}
    </script>
</body>

But after I press "click me!" button table not displayed.

Why table not displayed what I am missing here?

Upvotes: 0

Views: 59

Answers (1)

Oleksandr T.
Oleksandr T.

Reputation: 77482

#1 
function CreatTable() {
  var table = document.createElement('table');

  document.getElementById('ShowDataID').appendChild(table);
}

#2 
function CreatTable() {
  var _tmp  = document.createElement('div'),
      table = document.createElement('table');

  _tmp.appendChild(table);

  document.getElementById('ShowDataID').innerHTML = _tmp.innerHTML;
}

http://jsbin.com/soceca/1/

Upvotes: 2

Related Questions