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
Solved

Auto Increment Field Name on Add

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

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Open iframe using jquery dialog box 13 38
CORS Error from Chrome, but FireFox is OK 1 80
Html Newline 7 20
really easy css change help please 4 18
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…
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)

856 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