Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>sample HTML:</p> <pre><code>&lt;table id="mytable"&gt; &lt;tbody&gt; &lt;tr id="row1"&gt;&lt;td&gt;xxx&lt;/td&gt;&lt;td&gt;Bar&lt;/td&gt;&lt;/tr&gt; &lt;/tbody&gt; &lt;/table&gt; &lt;a href="#" onclick="javascript:InsertBefore();return false;"&gt;Insert Before&lt;/a&gt;&lt;br/&gt; &lt;a href="#" onclick="javascript:AppendChild();return false;"&gt;Append Child&lt;/a&gt;&lt;br/&gt; &lt;a href="#" onclick="javascript:InsertRow();return false;"&gt;InsertRow&lt;/a&gt; </code></pre> <p>samle SCRIPT:</p> <pre><code>var i=0; function randColor() { var str=Math.round(16777215*Math.random()).toString(16); return "#000000".substr(0,7-str.length)+str; } function InsertBefore() { var table = document.getElementById("mytable"); var under = document.getElementById("row1"); var newJob = document.createElement("tr"); newJob.style.backgroundColor=randColor(); //newJob.innerHTML = "&lt;tr&gt;&lt;td&gt;Foo&lt;/td&gt;&lt;td&gt;Bar&lt;/td&gt;&lt;/tr&gt;"; // its inserted inside TR. no additional TR's needed. newJob.innerHTML = "&lt;td&gt;Foo "+(i++)+".&lt;/td&gt;&lt;td&gt;Bar&lt;/td&gt;"; table.tBodies[0].insertBefore(newJob,under); } function AppendChild() { var table = document.getElementById("mytable"); var newJob = document.createElement("tr"); newJob.style.backgroundColor=randColor(); newJob.innerHTML = "&lt;td&gt;Foo "+(i++)+".&lt;/td&gt;&lt;td&gt;Bar&lt;/td&gt;"; table.tBodies[0].appendChild(newJob); } function InsertRow() { var indexToInsert=1; var table = document.getElementById("mytable"); var newJob = table.tBodies[0].insertRow(indexToInsert); newJob.style.backgroundColor=randColor(); newJob.innerHTML = "&lt;td&gt;Foo "+(i++)+".&lt;/td&gt;&lt;td&gt;Bar&lt;/td&gt;"; } </code></pre> <p>TR is a "table row". TR-elements can be appended only into TABLE, TBODY, THEAD, TFOOT elements.</p> <p>Find for <em>appendChild()</em> and <em>insertRow()</em> methods in MDN and MSDN</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload