Dynamic JS form fields with arbitrary add and remove

Hello,

I'm looking for some javascript to drive a dynamic portion of a POST form.  I'm using PHP and smarty for templating, and I need to be able to submit an arbitrary number of attribute id/name/value tuples along with the form, so am looking to use input arrays.  The attribute id/name list is pre-defined.  Most of what I've found so far is geared towards adding/removing input fields sequentially, but I need something slightly different.

Once the add button is clicked, the script should append another tr section to the div using the data in the input fields addAttrName and addAttrValue (these fields are ignored on form submit).  A non-zero addAttrName id and a non-null addAttrValue are required, and all data being added to the tr should be properly escaped, but other that that, there's not much in the way of restrictions (i.e. attribute name/ids may occur more than once, and the input arrays can also be empty/undefined).

I also need the ability to remove individual elements from the input array (ie onclick event for the delete button for each row).

I'd prefer not to use external JS frameworks if possible, but any help is appreciated.  Here's the relevant portion of my HTML with a snippet of smarty to illustrate the structure:

// PAGE AND TABLE SETUP

// OTHER FORM FIELDS

<div id='otherAttributes'>
	<tr>
		<th>Attribute</th>
		<th>Value</th>
		<th>Action</th>
	</tr>
	{foreach from=$attrs item=tuple}
	<tr>
		<input type="hidden" name="attrIds[]" value="{$tuple.id}" />
		<input type="hidden" name="attrValues[]" value="{$tuple.value|escape:'html'}" />
		<td>{$tuple.name|escape:'html'}</td>
		<td>{$tuple.value|escape:'html'}</td>
		<td><button type="button" onclick="">Delete</button></td>
	</tr>
	{/foreach}
</div>
	<tr>
		<td>
			<select name="addAttrName">
				<option value="0">Select an Attribute</option>
				<option value="1">attribute1</option>
				<option value="2">attribute2</option>
				<option value="3">attribute3</option>
			</select>
		</td>
		<td><input type="text" name="addAttrValue /></td>
		<td><button type="button" onclick="">Add</button></td>
	</tr>

// PAGE AND TABLE FOOTER

Open in new window

LVL 5
group0Asked:
Who is Participating?
 
ProculopsisConnect With a Mentor Commented:

You could refactor it to be non-jQuery:

<html>
<head>
<title>http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_26886354.html</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js" type="text/javascript"></script>
<script>

$(document).ready(function() {
  $(".delete").click( del );
});

function add() {
  var clone = $(".clone-me").clone();
  clone.removeClass("clone-me").show();
  $(".tuple-name", clone).text( $("#addAttrName").val() );
  $(".tuple-value", clone).text( $("#addAttrValue").val() );
  $(".delete", clone).bind( "click", del );
  $(".clone-me").before(clone);
}

function del() {
  $(this).closest("tr").remove();
}

</script>
</head>
<body>

<table>
	<tr>
		<th>Attribute</th>
		<th>Value</th>
		<th>Action</th>
	</tr>

	<tr>
		<input type="hidden" name="attrIds[]" value="{$tuple.id}" />
		<input type="hidden" name="attrValues[]" value="{$tuple.value|escape:'html'}" />
		<td class="tuple-name">{$tuple.name|escape:'html'}</td>
		<td class="tuple-value">{$tuple.value|escape:'html'}</td>
		<td><button class="delete" type="button">Delete</button></td>
	</tr>

	<tr class="clone-me" style="display: none;">
		<input type="hidden" name="attrIds[]" value="{$tuple.id}" />
		<input type="hidden" name="attrValues[]" value="{$tuple.value|escape:'html'}" />
		<td class="tuple-name">{$tuple.name|escape:'html'}</td>
		<td class="tuple-value">{$tuple.value|escape:'html'}</td>
		<td><button class="delete" type="button">Delete</button></td>
	</tr>

	<tr>
		<td>
			<select id="addAttrName" name="addAttrName">
				<option value="0">Select an Attribute</option>
				<option value="1">attribute1</option>
				<option value="2">attribute2</option>
				<option value="3">attribute3</option>
			</select>
		</td>
		<td><input type="text" id="addAttrValue" name="addAttrValue" /></td>
		<td><button type="button" onclick="add()">Add</button></td>
	</tr>
</table>


</body>
</html>

Open in new window

0
 
Maverick_CoolCommented:
you also use plain javascript:
domobject.createElement()
domobject.appendElement()
domobject.removeElement()
aftering creating the element you can specify its attributes also like id,.. and other custom attributes too.
0
 
group0Author Commented:
Thanks, I'll check this out and see if this works.
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
group0Author Commented:
@Proculopsis, this way seems relatively clean, however the dynamically inserted form fields don't end up in the final POST request for some reason, although I can see the new tr's are added properly when viewing the page with Firebug.  Any ideas?
0
 
group0Author Commented:
Figured this out, apparently opening the form tag immediately inside the table prevents the DOM from getting refreshed properly by jQuery.  Moving the form tag outside of the table makes it work perfectly.
0
 
group0Author Commented:
Great example, thanks for the help!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.