Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Auto Increment Field Name on Add

Posted on 2013-10-24
4
Medium Priority
?
924 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:Nathan Riley
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 58

Assisted Solution

by:Gary
Gary earned 1000 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 44

Accepted Solution

by:
Chris Stanyon earned 1000 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 12

Author Comment

by:Nathan Riley
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 12

Author Comment

by:Nathan Riley
ID: 39605950
Thanks!
0

Featured Post

Automating Terraform w Jenkins & AWS CodeCommit

How to configure Jenkins and CodeCommit to allow users to easily create and destroy infrastructure using Terraform code.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

721 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