Solved

Auto Increment Field Name on Add

Posted on 2013-10-24
4
888 Views
Last Modified: 2013-10-28
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
Comment
Question by:N R
  • 2
4 Comments
 
LVL 58

Assisted Solution

by:Gary
Gary earned 250 total points
ID: 39599139
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
 
LVL 43

Accepted Solution

by:
Chris Stanyon earned 250 total points
ID: 39599260
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
 
LVL 11

Author Comment

by:N R
ID: 39599369
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
 
LVL 11

Author Comment

by:N R
ID: 39605950
Thanks!
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
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.
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

773 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