Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 929
  • Last Modified:

Auto Increment Field Name on Add

I have a field that I can dynamically add to my form by pressing "Add Field"

http://jsfiddle.net/t7tN4/2/

How can I make the name of the new field go up in increments on 1 as I continue to add them?
0
Nathan Riley
Asked:
Nathan Riley
  • 2
2 Solutions
 
GaryCommented:
jsfiddle isn't working

Set a global var - that is a variable outside of any functions and then just increment it each time you add a new element.
e.g.
var mycounter=1;
function name(){
     // your jquery functions adding the element.
     // how you set the name will depend on how you are creating the element
     // and then...
     mycounter++;
}
0
 
Chris StanyonCommented:
Here's an alternative way. Rather than naming your inputs textbox1, textbox2, textbox3 etc, I've named them using an array textbox[1]. textbox[2], textbox[3] etc. This allows you to loop through $_POST['textbox'] to grab the values. It sets the array key by counting how many textboxes already exist:

http://jsfiddle.net/ChrisStanyon/8ZYuF/
0
 
Nathan RileyFounder/CTOAuthor Commented:
Ok thanks, the array is probably the better solution for me.  Since the name changes on all of them and is dynamic what would I use to add a little "remove" text to the right of the input box and remove it on click?

I opened a new question for this:
http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Jquery/Q_28276867.html
0
 
Nathan RileyFounder/CTOAuthor Commented:
Thanks!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now