Link to home
Start Free TrialLog in
Avatar of group0
group0

asked on

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

ASKER CERTIFIED SOLUTION
Avatar of Proculopsis
Proculopsis

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Avatar of group0
group0

ASKER

Thanks, I'll check this out and see if this works.
Avatar of group0

ASKER

@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?
Avatar of group0

ASKER

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.
Avatar of group0

ASKER

Great example, thanks for the help!