GuardianZ Posted January 6, 2006 Share Posted January 6, 2006 I have written some code to insert a row to a table, but how do i:1) set the class="classname" for the <tr> for the new row 2) after the button is clicked to insert a new row I want the html to be updated to reflect the new row(s).. Link to comment Share on other sites More sharing options...
aspnetguy Posted January 6, 2006 Share Posted January 6, 2006 please post the code you already have...I can make some modifications to that to acheive what you want. Link to comment Share on other sites More sharing options...
GuardianZ Posted January 9, 2006 Author Share Posted January 9, 2006 this is the javascript function I used: function insertrow() { var x=document.getElementById("maintable").insertRow(2); x.bgColor="#C0C9D4"; x.innerHTML="<td class='ns'><input type='button' onclick='insertrow()' value='+'/></td> \ <td>1</td> \ <td class='act'><textarea wrap='virtual' cols=28 rows=1 onchange='checksize(this)' onKeyUp='inc_lines(this)' name='act' type='text' id='text1'></textarea></td> \ <td class='in'><textarea wrap='virtual' cols=28 rows=1 onchange='checksize(this)'onKeyUp='inc_lines(this)' name='in' id='inp'></textarea></td> \ <td class='ex'><textarea wrap='virtual' cols=28 rows=1 onchange='checksizethis)'onKeyUp='inc_lines(this)' name='ex'id='er'></textarea></td> \ <td><input type='checkbox' /></td> \ <td><input id='fail' type='checkbox' onMouseUp='alertId2()'/></td> \ <td><input type='checkbox' /></td> \ <td class='com'><textarea wrap='virtual' cols=28 rows=1 onchange='checksize(this)'onKeyUp='inc_lines(this)' id='com' ></textarea></td> \ <td class='bi'><textarea wrap='virtual' cols=28 rows=1 onchange='checksize(this)'onKeyUp='inc_lines(this)' name='bugid' id ='bi'></textarea></td></tr>"; } it is called with <td class="ns"><form><input type="button" onclick="insertrow()" value="+"/></form></td> Link to comment Share on other sites More sharing options...
aspnetguy Posted January 9, 2006 Share Posted January 9, 2006 you can easily set the classname by x.className = 'theClassName'; this is how you create the elements and have them display <html><head><title>x</title><script type="text/javascript">function insertrow() { var x=document.getElementById("maintable"); var newRow = x.insertRow(2); newRow.style.background="#C0C9D4"; var td1 = document.createElement("TD") td1.appendChild(document.createTextNode("column 1")) newRow.appendChild(td1);}</script></head><body><table id="maintable"><tr><td class="ns"><form><input type="button" onclick="insertrow()" value="+"/></form></td></tr></table></body></html> You are creating a row so you have to create the columns aswell (you cannot use row.innerHTML) then you can put the proper text in the columns you created. Link to comment Share on other sites More sharing options...
GuardianZ Posted January 12, 2006 Author Share Posted January 12, 2006 I have managed to add a row and TD successfully, but when i click "View source" the new row is not reflected in the source code. Link to comment Share on other sites More sharing options...
aspnetguy Posted January 12, 2006 Share Posted January 12, 2006 That is because the javascript added it. It is not essentially part of the document. If you were to refresh the page it would be gone. There is no way to add it to the source code with javascript.For that you will need a server side language.Why does it have to go into the source code? Are you intending to save the source code to a different file or something? Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now