PDA

View Full Version : How to increment value in hidden form field



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

Old Pedant
Nov 19th, 2009, 11:04 PM
Try this:


var countfield = document.forms[0].hiddenCount;
countfield.value = 1 + parseInt("0" + countField.value, 10);

That works regardless of whether you initialize the hidden field's value to zero or not.

Old Pedant
Nov 19th, 2009, 11:06 PM
Wait a minute...you already *HAVE* a counter. Why not just store it in the hidden field???



var contact_counter = 0;
function add_contact()
{
if (contact_counter < 9)
{
contact_counter++;
document.forms[0].hiddenCount.value = contact_counter;
... etc. ...

Philip M
Nov 20th, 2009, 08:41 AM
<input id="add_contact()" onclick="add_contact()" value="Add" type="button">

Why if (contact_counter < 9) { // max value = 8?

gloopy
Nov 20th, 2009, 08:53 PM
Thank you for your help and advice - I really appreciate it.
It's working fine now.
Thank you!

gloopy
Nov 20th, 2009, 08:55 PM
<input id="add_contact()" onclick="add_contact()" value="Add" type="button">

Why if (contact_counter < 9) { // max value = 8?

Sorry to be dim Philip, but I don't understand your question.

Have I done something wrong?

Thanks

Old Pedant
Nov 20th, 2009, 09:26 PM
Philip is pointing out that the maximum value of your counter will be 8.

If you intended to make 9 the max, use <= 9 or < 10.

Philip M
Nov 21st, 2009, 09:00 AM
Sorry to be dim Philip, but I don't understand your question.

Have I done something wrong?

Thanks

<input id="add_contact()"
is not a valid id. id must begin with a letter A-Z or a-z
Can be followed by: letters (A-Za-z), digits (0-9), hyphens ("-"), underscores ("_"), colons (":"), and periods (".") But not brackets.

Even if it were valid it would not be sensible to use a function name as an id.

Old Pedant
Nov 21st, 2009, 09:20 AM
LOL! Now my eyes are going almost as bad as my brain.

Where did I leave my cane?

Philip M
Nov 21st, 2009, 09:29 AM
LOL! Now my eyes are going almost as bad as my brain.

Where did I leave my cane?

Well, your mother did warn you, you know! :D