Helping ordinary people create extraordinary websites!

Go Back   Web Development Forum > Website Programming > Server-Side Scripting
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-15-2005, 04:39 PM
Junior Member
 
Join Date: Jul 2005
Posts: 2
Default addRowToTable

Hello All.
The following code successfully adds 1 'TD' CELL into each ROW. I want to add be able to add more than 2 Cells. Like this:
"<tr><td>1</td><td>input 2></td><td><input 3></td></tr>". Can you help? Thanks.

PHP Code:
<script language="javascript">
// Last updated 2005-05-26
function addRowToTable()
{
var 
tbl document.getElementById('tblSample');
var 
lastRow tbl.rows.length;
// if there's no header row in the table, then iteration = lastRow + 1
var iteration lastRow;
var 
row tbl.insertRow(lastRow);
 
// left cell
var cellLeft row.insertCell(0);
var 
textNode document.createTextNode(iteration);
cellLeft.appendChild(textNode);
 
// right cell
var cellRight row.insertCell(1);
var 
el document.createElement('input');
el.setAttribute('type''text');
el.setAttribute('name''txtRow' iteration);
el.setAttribute('id''txtRow' iteration);
el.setAttribute('size''40');
el.onkeypress keyPressTest;
cellRight.appendChild(el);
}
function 
keyPressTest(eobj)
{
var 
validateChkb document.getElementById('chkValidateOnKeyPress');
if (
validateChkb.checked) {
    var 
displayObj document.getElementById('spanOutput');
    var 
key;
    if(
window.event) {
     
key window.event.keyCode
    }
    else if(
e.which) {
     
key e.which;
    }
    var 
objId;
    if (
obj != null) {
     
objId obj.id;
    } else {
     
objId this.id;
    }
    
displayObj.innerHTML objId ' : ' String.fromCharCode(key);
}
}
</script> 
</head>
<body>
<form action="tableaddrow_nw.html" method="get">
<p>
<input type="button" value="Add" onclick="addRowToTable();" />
<input type="button" value="Remove" onclick="removeRowFromTable();" />
<p>
<input type="checkbox" id="chkValidateOnKeyPress" checked="checked" /> Display OnKeyPress
<span id="spanOutput" style="border: 1px solid #000; padding: 3px;"> </span>
</p>
<table border="1" id="tblSample">
<tr>
<th colspan="2">Sample table</th>
</tr>
<tr>
<td>1</td>
<td><input type="text" name="txtRow1"
id="txtRow1" size="40" onkeypress="keyPressTest(event, this);" /></td>
</tr>
</table>
</form>
 
 
</body>
</html> 
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 02:50 AM.


Website Design by Ducani Media Group
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.