gloopy
Nov 19th, 2009, 10:14 PM
I have modified a free JS function from here:
http://www.tangorangers.com/examples/dynadddynpost/index.php
To dynamically add text fields to the form. My work-in-progress version is here:
http://jimpix.co.uk/junk/test/6.html
On the form, I have a hidden text field:
<input type="hidden" name="hiddenCount" value="" />
What I'd like to do is to increment the hiddenCount each time the "Add" fields button is clicked.
This is the button:
<input id="add_contact()" onclick="add_contact()" value="Add" type="button">
And this is the JS function:
var contact_counter = 0;
function add_contact()
{
if (contact_counter < 9)
{
contact_counter++;
var newFields = document.getElementById('add_contact').cloneNode(true);
newFields.id = 'contact';
newFields.style.display = 'block';
var newField = newFields.childNodes;
for (var i=0;i<newField.length;i++) {
var theName = newField[i].name
if (theName)
newField[i].name = theName + contact_counter;
}
var insertHere = document.getElementById('add_contact');
insertHere.parentNode.insertBefore(newFields,insertHere);
}
}
I'm new to JS. I could just about edit the JS function to slightly modify it from the tangorangers version (I only changed it v. slightly).
To somehow increment the value in the hidden field each time the button is pressed is beyond me though!
Any advice much appreciated.
Thanks
http://www.tangorangers.com/examples/dynadddynpost/index.php
To dynamically add text fields to the form. My work-in-progress version is here:
http://jimpix.co.uk/junk/test/6.html
On the form, I have a hidden text field:
<input type="hidden" name="hiddenCount" value="" />
What I'd like to do is to increment the hiddenCount each time the "Add" fields button is clicked.
This is the button:
<input id="add_contact()" onclick="add_contact()" value="Add" type="button">
And this is the JS function:
var contact_counter = 0;
function add_contact()
{
if (contact_counter < 9)
{
contact_counter++;
var newFields = document.getElementById('add_contact').cloneNode(true);
newFields.id = 'contact';
newFields.style.display = 'block';
var newField = newFields.childNodes;
for (var i=0;i<newField.length;i++) {
var theName = newField[i].name
if (theName)
newField[i].name = theName + contact_counter;
}
var insertHere = document.getElementById('add_contact');
insertHere.parentNode.insertBefore(newFields,insertHere);
}
}
I'm new to JS. I could just about edit the JS function to slightly modify it from the tangorangers version (I only changed it v. slightly).
To somehow increment the value in the hidden field each time the button is pressed is beyond me though!
Any advice much appreciated.
Thanks