Solved

Auto Increment Field Name on Add

Posted on 2013-10-24
4
873 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 42

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now