Solved

Dynamic JS form fields with arbitrary add and remove

Posted on 2011-03-14
6
715 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
Comment Utility

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
Comment Utility
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
Comment Utility
Thanks, I'll check this out and see if this works.
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 5

Author Comment

by:group0
Comment Utility
@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
Comment Utility
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
Comment Utility
Great example, thanks for the help!
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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 …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now