Re: Get value of text box, print out x times
I don't think I understand the use of "table name", but I guess I understand now the purpose.
You might want to change this script:
[code:2qafr8pr]
<script type="text/javascript">
function testResults (form) {
var numba = form.rownum.value;
for (i = 0; i < numba; i++){
document.write("Row" + i);
document.write("<br />");
}
</script>
[/code:2qafr8pr]
...into:
[code:2qafr8pr]
<script type="text/javascript">
function testResults (form) {
ray.addRow(form); // Pass to addRow method of ray object the form for reference
}
/**
Code Title: Add Row [input base]
Author: Raymond Angana as first seen on devppl.com/forum [username = rangana]
Date Created: 1/20/09
This notice must stay intact for legal use
*/
var ray={
addRow:function(el){
var tableEL = el.getElementsByTagName('table')[0]; // Get reference to the table
var cell = tableEL.getElementsByTagName('tr')[tableEL.getElementsByTagName('tr').length-1].getElementsByTagName('td')[0].innerHTML; // Get the innerHTML of the first TD of the last row
var numRow = el.elements['numrows'],i,base; // Initialise i and base variable and the numrows element
if(isNaN(Number(numRow.value))){ // If not a number
alert('I need a number!'); // Let them know it's not a number
return false; // End the function
}
if(cell.toLowerCase()=='number of rows:'){ // If a virgin (first time to click "add rows")
i = 1; // Initialise i var to 1
base = Number(numRow.value); // Get the number of inputs
}
else{ // Otherwise (no longer a virgin)
i = Number(cell.split('row')[1].split('"')[0])+1; // Get the last number to increment
base = Number(numRow.value)+i; // Initialise the number of input for a better loop
}
for(i;i<=base;i++){
var lastRow = tableEL.insertRow(-1); // Insert new row
var newCell = lastRow.insertCell(-1); // Insert new cell to the new row
newCell.innerHTML='<label for="row'+i+'">Row '+i+' Name: </label>'; // the first content of the first cell
newCell = lastRow.insertCell(-1); // Create another cell
newCell.innerHTML='<input type="textbox" name="row'+i+'">'; // Content of the 2nd cell
} // End of the for loop
}
}
</script>
[/code:2qafr8pr]
The features of the script are as follows:
1. Checks if the input is a number, otherwise, it will ask for a number
2. The script is witty enough to do a correct increment everytime you click on "Add Rows"
3. DOM
If you observed any oddity on the script, please don't hesitate to let me know.
Hope that helps.