/    Sign up×
Community /Pin to ProfileBookmark

Adding fields using Javascript

Hello, I have been looking all over for some help on this mater..

I am using javascript to add extra field to my form such as:
================================

Item: (input field) Price: (input field)

(link to add more input fields)
================================

the javascript is working great and it adds the two fields every time i click add more.

now the problem,

If i add one line of “item, and Price” then enter values in to the fields, then click on the link to add more fields it will take the “price” value and put it in the “item” field. So now it will have the value i put in the “price” in both field “item, and Price”.

How can i stop this and have the fields keep there values as i add more inputs?

here is the javascript i am using;
==============================

var arrInput = new Array(1);
var arrInputValue = new Array(1);

function addInput() {
arrInput.push(arrInput.length);
arrInputValue.push(“”);
display();
}

function display() {
document.getElementById(‘parah’).innerHTML=””;
for (intI=1;intI<arrInput.length;intI++) {
document.getElementById(‘parah’).innerHTML+=createInput(arrInput[intI], arrInputValue[intI]);
}
}

function saveValue(intId,strValue) {
arrInputValue[intId]=strValue;
}

function createInput(id,value) {
return “Item: <input type=’text’ name=’item[“+ id +”]’ id=’item”+ id +”‘ onChange=’javascript:saveValue(“+ id +”,this.value)’ value='”+ value +”‘> Price: <input type=’text’ name=’price[“+ id +”]’ id=’price”+ id +”‘ onChange=’javascript:saveValue(“+ id +”,this.value)’ value='”+ value +”‘><br>”;
}

function deleteInput() {
if (arrInput.length > 0) {
arrInput.pop();
arrInputValue.pop();
}
display();

}
=========================================

Thank you very much!

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@gwinnauthorApr 29.2007 — I found a solution... I used a new JavaScript... It will allow many fields at once and it wil keep the value in each field...

here it is.. I spent way to many hours on it, hope this will help someone!
==========================================


var counter = 0;

function init() {

document.getElementById('moreFields').onclick = moreFields;

moreFields();

}

function moreFields() {

counter++;

var newFields = document.getElementById('readroot').cloneNode(true);

newFields.id = '';

newFields.style.display = 'block';

var newField = newFields.childNodes;

for (var i=0;i<newField.length;i++) {

var theName = newField[i].name

if (theName)

newField[i].name = theName + "[" + counter + "]";

}

var insertHere = document.getElementById('writeroot');

insertHere.parentNode.insertBefore(newFields,insertHere);

}
Copy linkTweet thisAlerts:
@JMRKERApr 30.2007 — Last posting looks interesting.

Do you have an example of how it may be used?
Copy linkTweet thisAlerts:
@gwinnauthorApr 30.2007 — Hey, yes i found this:

http://www.quirksmode.org/dom/domform.html

that is what i am using now...

it will allow you to add fields in the form...
Copy linkTweet thisAlerts:
@JMRKERApr 30.2007 — Thanks for the information and location.
×

Success!

Help @gwinn spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...