Click to See Complete Forum and Search --> : variable contents as object reference


Mister_Magotchi
03-11-2003, 07:10 PM
I have a function with a variable in it.
I have form textbox elements that sends text to the variable, based on which form the textbox is in.

I have it to the point where my variable, after adding "document." to the beginning and ".value" to the end, has "document.formname.elementname.value" in it. I have tested this by setting other forms' element values to the the contents of the variable.

What I want to do is use the text that is in the variable as an object reference. Instead of the output that is put into the other forms' elements being "document.formname.elementname.value", I want it to read with the actual value of document.formname.elementname.value.

Just to help you know the direction I'm going with this script, I'm making it so when the user changes the text in one of my forms, it copies it to all of the rest of the forms. I have been able to do this before, but I didn't pass the form names from the forms, I just manually wrote everything out, which takes up lots of space.

If anyone can help me out with this, it would be greatly appreciated.

khalidali63
03-11-2003, 07:23 PM
If I understood your question correctly

here is what you do

var objRef = eval('document.formName.'+variable)
objRef.value and so on

Cheers

Khalid

pyro
03-11-2003, 07:24 PM
I don't think I understand you. Do you want to fill out other forms text fields automatically with the same value as a certain text field?

If so, try something like:

fieldval = document.formname.elementname.value;
document.formname2.elementname2.value = fieldval;

Mister_Magotchi
03-11-2003, 08:13 PM
I can't believe I didn't think of what you said, Pyro. I'm pretty sure that will work. Thank you. That it so simple it just amazes me.

pyro
03-11-2003, 08:14 PM
lol...:D Yes, if I understood you, that will work fine...

Cheers!

Mister_Magotchi
03-11-2003, 08:19 PM
I feel kind of stupid now though, after talking about how easy that was going to be. I could indeed manipulate that to work with what you told me, but whenever I add a new form to the page, I have to add new lines of code to the script to accommodate the new form. What I was trying to accomplish was to make the code do it dynamically rather than making a seperate function for each form like I had before.

pyro
03-11-2003, 08:24 PM
How are you going to be adding new forms? If you name your forms/fields in a sequention way, you could just use a loop, and loop through the forms/fields...

Mister_Magotchi
03-11-2003, 08:27 PM
I figured it out. It isn't exactly how I planned it, but it works great anyway. In each form, lets say the form was a google search box, I have it say:

onchange=copier(document.google.q.value);

This passes the value of that form to the copier function's variable called "textboxpointer". Then I just have to have each of the other forms each being set to the value of textboxpointer. Whenever I add a new form, I only need to add one line to my script with the address of the form, and I need to edit its onchange value.

pyro
03-11-2003, 08:31 PM
Ah, I thought you wanted to be able to dynamically add forms/fields and have them fill out no matter how many there were. Glad you got it working! ;)

Mister_Magotchi
03-11-2003, 08:32 PM
You might as well check out the work you helped create:

http://trio.trioserver.net/~tendolla/search/

pyro
03-11-2003, 08:40 PM
Looks good, but do you want to make it even better? Change all your onchange's to onkeyup. I bet you'll like that... :p

Mister_Magotchi
03-11-2003, 10:11 PM
cool, does that work for most browsers and versions of browsers?

pyro
03-11-2003, 10:47 PM
I don't exactly know what the browser compatibility issues are. I should think it would work in most browses made this century... :p