Solved

Dynamic JS form fields with arbitrary add and remove

Posted on 2011-03-14
6
720 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
Independent Software Vendors: 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!

 
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

Independent Software Vendors: 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

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
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…

680 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