gifford_c
asked on
Adding a row to a table and submit data
I'm new to JavaScript and I'm at the 1st stages of learning. I've got the script below that does almost everything I want but, I want the textboxes in a line rather than on top of each other? The submitting part works fine, its just if I move the textboxes next to each other I get error.
<html>
<head>
<script language="javascript">
function addRow(tbl){
var tb = tbl.firstChild;
var ind = tb.childNodes.length;
var nr = document.getElementById('c loneThis') .cloneNode (true);
nr.style.display = "block";
with(nr.firstChild.firstCh ild.firstC hild.child Nodes){
item(0).childNodes[1].inne rHTML = "<input type=text name=name" + ind + " />";
item(1).childNodes[1].inne rHTML = "<input type=text name=phone" + ind + " />"
}
tb.appendChild(nr);
if (tbl.rows.length > 1) {
document.getElementById('d elRow').di sabled = false;
}
document.forms[0].count.va lue = ind + 1;
}
function deletRow(tbl){
if (tbl.rows.length > 1) {
tblRow = tbl.deleteRow(tbl.rows.len gth-1);
// alert(tbl.rows.length);
}
}
function disablit(tbl){
if(tbl.rows.length == 1) {
document.getElementById('d elRow').di sabled = true;
}
}
</script>
</head>
<body>
<%
count = request.form("count")
for i = 0 to cint(count) - 1
name = request.form("name" & i)
phone = request.form("phone" & i)
response.write "name = " & name & "<br>"
response.write "phone = " & phone & "<br>"
next
%>
<form name="addAccount" method="post" >
<input type=hidden name="count" value="1">
<table id="purchase">
<tr id='cloneThis'>
<td>
<table>
<tr>
<td>Name</td>
<td colspan=5><input type="text" name="name0"></td>
</tr>
<tr>
<td>Phone</td>
<td colspan=5><input type="text" name="phone0"</td>
</tr>
</table>
</td></tr>
</table>
<input type=button onclick="addRow(document.g etElementB yId('purch ase'))" value="Add">
<input type=button id="delRow" name="delRow" onclick="deletRow(document .getElemen tById('pur chase'));d isablit(ad dContact); " value="Delete" disabled>
<br>
<br>
<input type="submit" value="submit">
</form>
</body>
</html>
<html>
<head>
<script language="javascript">
function addRow(tbl){
var tb = tbl.firstChild;
var ind = tb.childNodes.length;
var nr = document.getElementById('c
nr.style.display = "block";
with(nr.firstChild.firstCh
item(0).childNodes[1].inne
item(1).childNodes[1].inne
}
tb.appendChild(nr);
if (tbl.rows.length > 1) {
document.getElementById('d
}
document.forms[0].count.va
}
function deletRow(tbl){
if (tbl.rows.length > 1) {
tblRow = tbl.deleteRow(tbl.rows.len
// alert(tbl.rows.length);
}
}
function disablit(tbl){
if(tbl.rows.length == 1) {
document.getElementById('d
}
}
</script>
</head>
<body>
<%
count = request.form("count")
for i = 0 to cint(count) - 1
name = request.form("name" & i)
phone = request.form("phone" & i)
response.write "name = " & name & "<br>"
response.write "phone = " & phone & "<br>"
next
%>
<form name="addAccount" method="post" >
<input type=hidden name="count" value="1">
<table id="purchase">
<tr id='cloneThis'>
<td>
<table>
<tr>
<td>Name</td>
<td colspan=5><input type="text" name="name0"></td>
</tr>
<tr>
<td>Phone</td>
<td colspan=5><input type="text" name="phone0"</td>
</tr>
</table>
</td></tr>
</table>
<input type=button onclick="addRow(document.g
<input type=button id="delRow" name="delRow" onclick="deletRow(document
<br>
<br>
<input type="submit" value="submit">
</form>
</body>
</html>
I dont find any textbox in the code you have posted ???
Is this what you are looking for,
<html>
<head>
<script language="javascript">
function addRow(tbl){
var tb = tbl.firstChild;
var ind = tb.childNodes.length;
var nr = document.getElementById('c loneThis') .cloneNode (true);
nr.style.display = "block";
with(nr.firstChild.firstCh ild.firstC hild.child Nodes){
item(0).childNodes[1].inne rHTML = "<input type=text name=name" + ind + " />";
item(0).childNodes[1].inne rHTML = "<input type=text name=phone" + ind + " />"
}
tb.appendChild(nr);
if (tbl.rows.length > 1) {
document.getElementById('d elRow').di sabled = false;
}
document.forms[0].count.va lue = ind + 1;
}
function deletRow(tbl){
if (tbl.rows.length > 1) {
tblRow = tbl.deleteRow(tbl.rows.len gth-1);
// alert(tbl.rows.length);
}
}
function disablit(tbl){
if(tbl.rows.length == 1) {
document.getElementById('d elRow').di sabled = true;
}
}
</script>
</head>
<body>
<%
count = request.form("count")
for i = 0 to cint(count) - 1
name = request.form("name" & i)
phone = request.form("phone" & i)
response.write "name = " & name & "<br>"
response.write "phone = " & phone & "<br>"
next
%>
<form name="addAccount" method="post" >
<input type=hidden name="count" value="1">
<table id="purchase">
<tr id='cloneThis'>
<td>
<table>
<tr>
<td>Name</td>
<td colspan=5><input type="text" name="name0"></td>
<td>Phone</td>
<td colspan=5><input type="text" name="phone0"</td>
</tr>
</table>
</td></tr>
</table>
<input type=button onclick="addRow(document.g etElementB yId('purch ase'))" value="Add">
<input type=button id="delRow" name="delRow" onclick="deletRow(document .getElemen tById('pur chase'));d isablit(ad dContact); " value="Delete" disabled>
<br>
<br>
<input type="submit" value="submit">
</form>
</body>
</html>
<html>
<head>
<script language="javascript">
function addRow(tbl){
var tb = tbl.firstChild;
var ind = tb.childNodes.length;
var nr = document.getElementById('c
nr.style.display = "block";
with(nr.firstChild.firstCh
item(0).childNodes[1].inne
item(0).childNodes[1].inne
}
tb.appendChild(nr);
if (tbl.rows.length > 1) {
document.getElementById('d
}
document.forms[0].count.va
}
function deletRow(tbl){
if (tbl.rows.length > 1) {
tblRow = tbl.deleteRow(tbl.rows.len
// alert(tbl.rows.length);
}
}
function disablit(tbl){
if(tbl.rows.length == 1) {
document.getElementById('d
}
}
</script>
</head>
<body>
<%
count = request.form("count")
for i = 0 to cint(count) - 1
name = request.form("name" & i)
phone = request.form("phone" & i)
response.write "name = " & name & "<br>"
response.write "phone = " & phone & "<br>"
next
%>
<form name="addAccount" method="post" >
<input type=hidden name="count" value="1">
<table id="purchase">
<tr id='cloneThis'>
<td>
<table>
<tr>
<td>Name</td>
<td colspan=5><input type="text" name="name0"></td>
<td>Phone</td>
<td colspan=5><input type="text" name="phone0"</td>
</tr>
</table>
</td></tr>
</table>
<input type=button onclick="addRow(document.g
<input type=button id="delRow" name="delRow" onclick="deletRow(document
<br>
<br>
<input type="submit" value="submit">
</form>
</body>
</html>
ASKER
The text boxes are side by side by when you click on Add the phone entry isn't blank it copies what you put in the top box, also when you submit they are seperated by comma, which I dont want.
Just the same code I made yesterday :-)
http:Q_20929157.html
<html>
<head>
<title>Untitled</title>
<script language="javascript">
function addRow(){
var tr = document.getElementById('c loneThis') ;
var tb = tr.parentNode;
var ind = tb.rows.length;
var nr = tr.cloneNode(true);
nr.id = "R"+ind;
tb.appendChild(nr);
theForm = document.forms[0];
newFirstName = theForm.contactFName;
newLastName = theForm.contactLName;
newPhone = theForm.contactPhone;
newFirstName = newFirstName[newFirstName. length-1];
newFirstName.value = "";
newFirstName.name = "fname" + ind;
newFirstName.id = "fname" + ind;
newLastName = newLastName[newLastName.le ngth-1];
newLastName.value = "";
newLastName.name = "lname" + ind;
newLastName.id = "lname" + ind;
newPhone = newPhone[newPhone.length-1 ];
newPhone.value = "";
newPhone.name = "phone" + ind;
newPhone.id = "phone" + ind;
if (tb.rows.length > 2) {
var elem = document.getElementById('d elRow');
elem.disabled = false;
elem.style.display = "block";
}
}
function deletRow(tblName){
var tbl = document.getElementById(tb lName);
if (tbl.rows.length > 2) {
tblRow = tbl.deleteRow(tbl.rows.len gth-1);
if(tbl.rows.length < 3) {
var elem = document.getElementById('d elRow')
elem.disabled = true;
elem.style.display = "none";
}
}
}
</script>
</head>
<body>
<form>
<DIV>
<table cellspacing="0" cellpadding="3" border="1" width="100%">
<tr>
<td>
<table cellspacing="0" cellpadding="0" border="0" width="100%" id="addContact">
<tr>
<td colspan="2">Account Contact(s) Information</td>
</tr>
<tr id='cloneThis'>
<td colspan="2">
<table cellspacing="0" cellpadding="3" border="1" width="100%">
<tr>
<td>*Name</td>
<td colspan="5"><input type="text" name="contactFName" value="<%=contactFName%>"> &nbs p;LName&nb sp;<input type="text" name="contactLName" value="<%=contactLName%>"> </td>
</tr>
<tr>
<td>*Phone</td>
<td colspan="5"><input type="text" name="contactPhone" value="<%=contactPhone%>"> </td>
</tr>
<tr><td colspan="6"> </td></t r>
</table>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<div style="float:left;"><input type=button onclick="addRow('addContac t')" value="Add another contact?"></div>
<input type=button id="delRow" name="delRow" onclick="deletRow('addCont act');" value="Cancel" disabled style="display:none">
</td>
</tr>
</table>
</form>
</body>
</html>
http:Q_20929157.html
<html>
<head>
<title>Untitled</title>
<script language="javascript">
function addRow(){
var tr = document.getElementById('c
var tb = tr.parentNode;
var ind = tb.rows.length;
var nr = tr.cloneNode(true);
nr.id = "R"+ind;
tb.appendChild(nr);
theForm = document.forms[0];
newFirstName = theForm.contactFName;
newLastName = theForm.contactLName;
newPhone = theForm.contactPhone;
newFirstName = newFirstName[newFirstName.
newFirstName.value = "";
newFirstName.name = "fname" + ind;
newFirstName.id = "fname" + ind;
newLastName = newLastName[newLastName.le
newLastName.value = "";
newLastName.name = "lname" + ind;
newLastName.id = "lname" + ind;
newPhone = newPhone[newPhone.length-1
newPhone.value = "";
newPhone.name = "phone" + ind;
newPhone.id = "phone" + ind;
if (tb.rows.length > 2) {
var elem = document.getElementById('d
elem.disabled = false;
elem.style.display = "block";
}
}
function deletRow(tblName){
var tbl = document.getElementById(tb
if (tbl.rows.length > 2) {
tblRow = tbl.deleteRow(tbl.rows.len
if(tbl.rows.length < 3) {
var elem = document.getElementById('d
elem.disabled = true;
elem.style.display = "none";
}
}
}
</script>
</head>
<body>
<form>
<DIV>
<table cellspacing="0" cellpadding="3" border="1" width="100%">
<tr>
<td>
<table cellspacing="0" cellpadding="0" border="0" width="100%" id="addContact">
<tr>
<td colspan="2">Account Contact(s) Information</td>
</tr>
<tr id='cloneThis'>
<td colspan="2">
<table cellspacing="0" cellpadding="3" border="1" width="100%">
<tr>
<td>*Name</td>
<td colspan="5"><input type="text" name="contactFName" value="<%=contactFName%>">
</tr>
<tr>
<td>*Phone</td>
<td colspan="5"><input type="text" name="contactPhone" value="<%=contactPhone%>">
</tr>
<tr><td colspan="6"> </td></t
</table>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<div style="float:left;"><input
<input type=button id="delRow" name="delRow" onclick="deletRow('addCont
</td>
</tr>
</table>
</form>
</body>
</html>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.