Solved

Javascript: How to create a form with repeatable input fields?

Posted on 2009-05-14
6
321 Views
Last Modified: 2012-05-07
I have a html form with a drop down menu with a list of names. The user can select one of the names from this list, but I would like a button next to it that says "add another person" which will use javascript to add another identical drop down menu below it so they can choose as many people as they like.

Below I have put some example html. When the user clicks "Add Another" I want a new row to be inserted into the table with an identical dropdown menu.

Does anyone know how to do this?


<form id="form1" name="form1" method="post" action="">
  <table width="569" border="0">
    <tr>
      <td>Your Name  </td>
      <td><label>
        <input type="text" name="textfield" />
      </label></td>
    </tr>
    <tr>
      <td>Your Friend/s Name s </td>
      <td><label>
        <select name="select">
          <option value="jim">Jim</option>
          <option value="bob">Bob</option>
          <option value="joe">Joe</option>
        </select>
        <input type="button" name="add" value="Add Another Person" onclick="addAnother();" />
      </label></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
  </table>
</form>

Open in new window

0
Comment
Question by:divstar
[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
  • 4
  • 2
6 Comments
 
LVL 20

Expert Comment

by:ddayx10
ID: 24392845
Might be a little too cute on the DOM objects, but I knew whatever I built might not fit your idea of layout perfectly so I just thought I'd give you a sample.

dday
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
      <script type="text/javascript">
  function addAnother(btn)
  {
	var tr = document.getElementById('ParentRow');
	var newRow = tr.cloneNode('true');
	var noBtn = newRow.getElementsByTagName('input')[0];
	newRow.childNodes[1].firstChild.removeChild(noBtn);
	tr.parentNode.appendChild(newRow, tr.nextSibling);
	return false;
  }
  
  </script>
</head>
<body>
    <form id="form1" runat="server">
 
 <table width="569" border="0">
    <tr>
      <td>Your Name  </td>
      <td><label>
        <input type="text" name="textfield" />
      </label></td>
    </tr>
    <tr id="ParentRow">
      <td>Your Friend/s Name s </td>
      <td><label>
        <select name="select">
          <option value="jim">Jim</option>
          <option value="bob">Bob</option>
          <option value="joe">Joe</option>
        </select>
        <input type="button" name="add" value="Add Another Person" onclick="return addAnother(this);" />
      </label></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
  </table>
 
 
 
 
    </form>
</body>
</html>

Open in new window

0
 
LVL 2

Author Comment

by:divstar
ID: 24392923
This works great - if i wanted to later refer to each field say in a php script, what name/s would each succesive child be given? Im guessing $_POST['select'] would give me the value of the first row, what about the others?

Thanks
0
 
LVL 20

Expert Comment

by:ddayx10
ID: 24393068
Well.... couple things:

1) I dont do PHP, sorry.

2) Right now they dont have names. If you would like them to have names then we can do that.

3) I screwed it up a bit. It wasnt working right in FF (wrote too fast for my own good).

This code corrects the FF problem, and gives each subsequent row a  new name. In this case the names are sequential "row1, row2, etc".

And finally...I'm goin home. Wont be back till tommorrow afternoon. Hopefully this gets you on your way.

dday
0
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
LVL 20

Accepted Solution

by:
ddayx10 earned 125 total points
ID: 24393071
....obviously tired I forgot to attach the code!
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
<script type="text/javascript">
  //need a global counter
  var i = 1;
      
  function addAnother(btn)
  {
	var tr = document.getElementById('ParentRow');
	var newRow = tr.cloneNode('true');
	var newName = 'row' + i.toString();
	newRow.id = newName;
	var noBtn = newRow.getElementsByTagName('input')[0];
	noBtn.parentNode.removeChild(noBtn);
	newRow.setAttribute('name', newName);
	tr.parentNode.appendChild(newRow, tr.nextSibling);
	i++;
	return false;
  }
  
  </script>
</head>
<body>
    <form id="form1" runat="server">
 
 <table width="569" border="0">
    <tr>
      <td>Your Name  </td>
      <td><label>
        <input type="text" name="textfield" />
      </label></td>
    </tr>
    <tr id="ParentRow">
      <td>Your Friend/s Name s </td>
      <td><label>
        <select name="select">
          <option value="jim">Jim</option>
          <option value="bob">Bob</option>
          <option value="joe">Joe</option>
        </select>
        <input type="button" name="add" value="Add Another Person" onclick="return addAnother(this);" />
      </label></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
  </table>
 
 
 
 
    </form>
</body>
</html>

Open in new window

0
 
LVL 2

Author Comment

by:divstar
ID: 24393114
This is perfect, thankyou
0
 
LVL 20

Expert Comment

by:ddayx10
ID: 24393127
Your welcome.
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

Suggested Solutions

Title # Comments Views Activity
Aspx calendar pop up control 3 37
How do I post more than 1 item to php backend 24 60
Boolean 13 49
Drag & Drop Error 5 30
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult 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…
Suggested Courses

734 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