|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
Free Web 2.0 Code Generator! Generate data entry and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!
|
|
#1
|
|||
|
|||
|
addRowToTable
Need to create more than one TD Cell. The code below only creates One(well 2) but I need more than that. Any idea how to add more CElls? Thanks.
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <title>Untitled Document</title> <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(e, obj) { 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();" /> <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> |
|
#2
|
||||
|
||||
|
Using your code, I made this:
Code:
function addRowsToTable(n)
{
for (var i=0; i<n; i++) {
addRowToTable();
}
}
Code:
<div><input type="text" size="4" id="howMany" /><input type="button" value="Add" onclick="addRowsToTable(document.getElementById('howMany'). value);" /></div>
|
|
#3
|
|||
|
|||
|
can you explain
Hi, thank you for your post.
Just a question: Did you reduce PHP Code:
TO THIS???? PHP Code:
Can you explain how does this work? thanks. |
|
#4
|
||||
|
||||
|
lol, no no! sorry to confuse.
Your code didn't look to be that bad... afterall, it worked... AllI did was utilize the fact that you've already written the addRowToTable function. All I did was make a function, addRowsToTable, to simply loop your function over and over. You need both functions to be able to add multiple rows. Your function, addRowToTable(), adds a single row. My function, addRowsToTable(), calls your function many times. |
|
#5
|
|||
|
|||
|
Hi and thanks for trying to help but I might not have explained well.... I am trying to genearte this on the fly(the one that we have now only creates the first cell):
PHP Code:
Quote:
|
|
#6
|
||||
|
||||
|
I had assumed you wanted to prompt the user for how many thigns they wanted to add to the list, then generate that many text boxes.... no?
How will you determine how many boxes to display when the page loads? |
|
#7
|
|||
|
|||
|
Quote:
I don't know how its going to work out but I 'might' have a solution. Basically to add another Cell(or column) on each Row all I did was modify the addRowToTaBLE function like this(I am sure there is an easier way to do this but I don't know how): PHP Code:
What do you think? |
|
#8
|
||||
|
||||
|
So long as it performs what you need, I guess it's fine. =)
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > JavaScript Development > addRowToTable |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
![]() |