Solved

Adding a row to a table and submit data

Posted on 2004-03-24
5
835 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 8

Expert Comment

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

Expert Comment

by:william_jwd
ID: 10666308
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
ID: 10666451
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
ID: 10666539
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
ID: 10666625
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

749 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