Solved

Dynamic JS form fields with arbitrary add and remove

Posted on 2011-03-14
6
719 Views
Last Modified: 2012-05-11
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

0
Comment
Question by:group0
  • 4
6 Comments
 
LVL 20

Accepted Solution

by:
Proculopsis earned 500 total points
ID: 35135583

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
 
LVL 7

Expert Comment

by:Maverick_Cool
ID: 35136188
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
 
LVL 5

Author Comment

by:group0
ID: 35140882
Thanks, I'll check this out and see if this works.
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 5

Author Comment

by:group0
ID: 35141711
@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
 
LVL 5

Author Comment

by:group0
ID: 35142703
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
 
LVL 5

Author Closing Comment

by:group0
ID: 35142785
Great example, thanks for the help!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
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…

792 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