insert row in table using innerhtml

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dipalichavan82
    New Member
    • Feb 2008
    • 41

    insert row in table using innerhtml

    i m writing javascript.i hae a table.i want on button click, tr(this tr itself has another table, class for style) to be added as first row in table.

    i searched on net but didnt get gud solution. i want to use innerHTML or clonenode or createelement ......plz help
  • iam_clint
    Recognized Expert Top Contributor
    • Jul 2006
    • 1207

    #2
    Heres an example of using DOM createElement, and appending it to the table body.
    Code:
    <script>
    function addRow(tbody, text) {
    	var tr=document.createElement("TR");
    	var td=document.createElement("TD");
    	td.innerHTML = text;
    	tr.appendChild(td);
    	tbody.appendChild(tr);
    }
    </script>
    <input type="button" value="add row" onclick="addRow(document.getElementById('tbody'), 'This is the added row');">
    <table border="1">
    <tbody id="tbody">
    <tr><td>test1</td></tr>
    <tr><td>test2</td></tr>
    </tbody>
    </table>

    Comment

    • Rsmastermind
      New Member
      • Sep 2008
      • 93

      #3
      Hi

      I had done this in my project.This function dynamically add a row to your table.
      No need to worry about the structure.Like table inside the table just give the table name of that table in which you want to insert row.[HTML]
      Code:
      [B]
      
      
      [CODE=javascript]function abc(){
      var tableObject=document.getElementById("Id of the table");
      //alert(tableObject);
      	var rowCount=tableObject.rows.length;
      	tableObject.insertRow(rowCount);
      	if(rowCount%2 != 0)
      	{
      	tableObject.rows[rowCount].className="evenRow";//For class      the TR
      	}
      	else
      	{
      		tableObject.rows[rowCount].className="oddRow";
      	}
      	var column1=tableObject.rows[rowCount].insertCell(0);
      		column1.innerHTML="<td class='rightSeparator'><input id='selectCheckBox' name='selectCheckBox' type='radio'  /></td>";
      
      }
      Last edited by acoder; Sep 18 '08, 03:23 PM. Reason: Added [code] tags

      Comment

      • r37r0
        New Member
        • Sep 2008
        • 1

        #4
        Man that's awesome, it's as if Java DOM and innerHTML just became very popular, so many people working on the same issues. Been trolling google all night flirting with greasmonkey scripts..... ty Rmastermind!

        Comment

        Working...