Solved

Adding a row to a table and submit data

Posted on 2004-03-24
5
814 Views
Last Modified: 2008-02-01
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('cloneThis').cloneNode(true);
 nr.style.display = "block";
 with(nr.firstChild.firstChild.firstChild.childNodes){
   item(0).childNodes[1].innerHTML = "<input type=text name=name" + ind + " />";
   item(1).childNodes[1].innerHTML = "<input type=text name=phone" + ind + " />"
 }
 tb.appendChild(nr);
 if (tbl.rows.length > 1) {
   document.getElementById('delRow').disabled = false;
 }
 document.forms[0].count.value = ind + 1;
}

function deletRow(tbl){
  if (tbl.rows.length > 1) {
    tblRow = tbl.deleteRow(tbl.rows.length-1);
    //           alert(tbl.rows.length);
  }
}
function disablit(tbl){
  if(tbl.rows.length == 1) {
    document.getElementById('delRow').disabled = 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.getElementById('purchase'))" value="Add">
<input type=button id="delRow" name="delRow" onclick="deletRow(document.getElementById('purchase'));disablit(addContact);" value="Delete" disabled>
<br>
<br>
<input type="submit" value="submit">
</form>
</body>
</html>
0
Comment
Question by:gifford_c
  • 3
5 Comments
 
LVL 8

Expert Comment

by:william_jwd
Comment Utility
I dont find any textbox in the code you have posted ???
0
 
LVL 8

Expert Comment

by:william_jwd
Comment Utility
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('cloneThis').cloneNode(true);
 nr.style.display = "block";
 with(nr.firstChild.firstChild.firstChild.childNodes){
   item(0).childNodes[1].innerHTML = "<input type=text name=name" + ind + " />";
   item(0).childNodes[1].innerHTML = "<input type=text name=phone" + ind + " />"
 }
 tb.appendChild(nr);
 if (tbl.rows.length > 1) {
   document.getElementById('delRow').disabled = false;
 }
 document.forms[0].count.value = ind + 1;
}

function deletRow(tbl){
  if (tbl.rows.length > 1) {
    tblRow = tbl.deleteRow(tbl.rows.length-1);
    //           alert(tbl.rows.length);
  }
}
function disablit(tbl){
  if(tbl.rows.length == 1) {
    document.getElementById('delRow').disabled = 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.getElementById('purchase'))" value="Add">
<input type=button id="delRow" name="delRow" onclick="deletRow(document.getElementById('purchase'));disablit(addContact);" value="Delete" disabled>
<br>
<br>
<input type="submit" value="submit">
</form>
</body>
</html>
0
 

Author Comment

by:gifford_c
Comment Utility
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.
0
 
LVL 63

Expert Comment

by:Zvonko
Comment Utility
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('cloneThis');
      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.length-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('delRow');
        elem.disabled = false;
        elem.style.display = "block";
       }
    }


    function deletRow(tblName){
      var tbl = document.getElementById(tblName);
      if (tbl.rows.length > 2) {
        tblRow = tbl.deleteRow(tbl.rows.length-1);
        if(tbl.rows.length < 3) {
           var elem = document.getElementById('delRow')
           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%>">&nbsp;&nbsp;LName&nbsp;<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">&nbsp;</td></tr>
                             </table>
                        </td>
                   </tr>
              </table>
         </td>
    </tr>
    <tr>
         <td>
              <div style="float:left;"><input type=button onclick="addRow('addContact')" value="Add another contact?"></div>
             <input type=button id="delRow" name="delRow" onclick="deletRow('addContact');" value="Cancel" disabled style="display:none">
         </td>
    </tr>    
</table>
</form>
</body>
</html>

0
 
LVL 8

Accepted Solution

by:
william_jwd earned 250 total points
Comment Utility
Sorry gifford_c, it was my mistake not changing the childnodes index.  try this,

<html>
<head>
<script language="javascript">
function addRow(tbl){
 var tb = tbl.firstChild;
 var ind = tb.childNodes.length;
 var nr = document.getElementById('cloneThis').cloneNode(true);
 nr.style.display = "block";
 with(nr.firstChild.firstChild.firstChild.childNodes){
   item(0).childNodes[1].innerHTML = "<input type=text name=name" + ind + " />";
   item(0).childNodes[3].innerHTML = "<input type=text name=phone" + ind + " />"
 }
 tb.appendChild(nr);
 if (tbl.rows.length > 1) {
   document.getElementById('delRow').disabled = false;
 }
 document.forms[0].count.value = ind + 1;
}

function deletRow(tbl){
  if (tbl.rows.length > 1) {
    tblRow = tbl.deleteRow(tbl.rows.length-1);
    //           alert(tbl.rows.length);
  }
}
function disablit(tbl){
  if(tbl.rows.length == 1) {
    document.getElementById('delRow').disabled = 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 border=1>
<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.getElementById('purchase'))" value="Add">
<input type=button id="delRow" name="delRow" onclick="deletRow(document.getElementById('purchase'));disablit(addContact);" value="Delete" disabled>
<br>
<br>
<input type="submit" value="submit">
</form>
</body>
</html>
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now