javascript - When creating a table inside a div. Text "undefined" shows up for no reason -


i have table create in div. on button click table being created inside div. html code use create emtpy div, when page being opened:

<div id="historytablediv">     </div> 

this javascript code execute when button clicked create table inside div:

function createtable() {     var text = 'id: test<br>';     var thead = '<table><thead><tr><td>#</td><td>name</td></tr></thead><tbody>';     var tbody = '<tr>';     tbody += '<td>test id</td>';             tbody += '<td>test name</td>';     var tfooter = '</tr></tbody></table>';     document.getelementbyid('historytablediv').innerhtml = text + thead + tbody + tfooter; } 

the table created fine. problem text "undefined" showing between text "id: test" , table. have absolutely no idea coming from.

extra info: have 6 divs on page. different id's. use createtable() function. show same table in different divs , above every table text "undefined" shows up.

when inspect element see nodevalue attribute of div has value "undefined" when change text "test" text "test" shows instead of undefined.

there no problem in above code, may problem in other part. can share full html , js code?

the following code works fine me.

   <html>          <head></head>          <body>              <p><button onclick="createtable()">create table</button></p>              <div id="historytablediv">              </div>              <script type="text/javascript">                  function createtable() {                      var text = 'id: test<br>';                      var thead = '<table><thead><tr><td>#</td><td>name</td></tr></thead><tbody>';                      var tbody = '<tr>';                      tbody += '<td>test id</td>';                              tbody += '<td>test name</td>';                      var tfooter = '</tr></tbody></table>';                      document.getelementbyid('historytablediv').innerhtml = text + thead + tbody + tfooter;                  }              </script>          </body>      </html>


Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -