Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Adding a row to a table and submit data

Posted on 2004-03-24
5
Medium Priority
?
854 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 1000 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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
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…

661 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