PDA

View Full Version : Append Variable Name to Form Object



gotg
May 2nd, 2005, 06:44 PM
Background
I've created a simple form to allow users to edit a list of names. The names appear in textareas and are disabled by default. The user clicks an 'Edit' button next to the appropriate field to enable the field for editing. The form is at: www.girlonthegallows.com/jobs/creditsform.html

Problem
Each textarea is named credit1, credit2, etc. and I want to have a single function to enable to field. I can succesfully pass the variable to a function but can't figure out how to append the variable to reference the correct teaxtarea.

Here's the code that writes the textareas into the form ('CreditsList' is an array that writes text into the teaxtarea):

for (i=1; i<CreditsList.length; i++) {
document.write("<textarea name='credit" + [i] + "' cols='40' rows='2' disabled='disabled'>")
document.write(CreditsList[i])
document.write("</textarea>")
document.write("<input type='button' value='Edit' onclick='JavaScript:editField(" + [i] + ")' name='field" + [i] + "'>")
document.write("<br />")
}

Here's the function I want to pass the variable to:

function editField(variable) {
document.creditsForm.credit[variable].disabled = false
}

It seems like it should be simple but I can't get it to work unless I create a separate function for each textarea.

Any help would be appreciated. Thanks, Andre

SpirtOfGrandeur
May 2nd, 2005, 07:20 PM
Youa re refrencing an array but do not actually have an array. You are creating each element:

credit1
credit2
credit3

That does not actually create an array of the element like you are trying in the line 'document.creditsForm.credit[variable].disabled = false'. You could either remove the number at the end of the name. Or you could call each one by its actual name.

in your case if you switched to getElementById('credit' + variable) and sent the string it should work!

*edit*
you would need to add the ID tag also to your textarea.

gotg
May 2nd, 2005, 09:18 PM
Thanks! The getElementByID worked like a charm and even gave me some other ideas on how to make it more efficient.