Click to See Complete Forum and Search --> : wheres tham thar elements ?!?!?
alex_read
Jan 17th, 2002, 10:10 AM
Function CalledOn(elementTxtBox)
{
var paramElementTxtBox = form.elements.item(elementTxtBox);
alert(elementTxtBox.value)
}
FunctionProper() {
CalledOn(document.form.item("text1"))
}
I want to try & pass an element into a function, and call on it's properties & methods (bit like vb's Controls collection). Apparently this is possible in Javascript, using the name of the element rather than the element number/index.
Anyone got any ideas how I can do this one please ?!?!?!?!
:confused: :eek: ;) :confused: Cheers!
Cudabean
Jan 17th, 2002, 12:53 PM
It's easy to declare an object with properties in JavaScript--you don't. Instead, you just start using the properties in your code:
elementTxtBox.name = "myControl";
elementTxtBox.value = "Yes";
Unfortunately, I can't help you with defining methods for elementTxtBox.
cudabean
Cudabean
Jan 17th, 2002, 02:29 PM
Oops, made a mistake. It's not quite that easy:
elementTxtBox.name = new Object();
elementTxtBox.name = "myControl";
elementTxtBox.value = "Yes";
cudabean
Cudabean
Jan 17th, 2002, 02:31 PM
Grrr. This is finally correct:
elementTxtBox = new Object();
elementTxtBox.name = "myControl";
elementTxtBox.value = "Yes";
alex_read
Jan 17th, 2002, 03:19 PM
Thanks cudabean,
If I understand this right though, you're defining a new element on the page?
My idea (if it's possible) is to use an existing one. I could use :
FunctionProper() {
alert(document.forms.elements.item("text1"))
}In the actual function, but if I had 5 function doing the same thing, I was hoping to make a general function with the messagebox/alert code in, and just call this from the 5 functions I need, passing in the name of the element/textbox I want to display the value of. :)
Not sure if that one made too much sense there, sorry.
Thanks! ;)
Cudabean
Jan 17th, 2002, 05:03 PM
I defined a new object that doesn't relate to anything on the page. I think I misunderstood your original premise. The object I created is only used to store data in a structure.
In looking back at your original message I think I understand better.
I think your original code is close to correct, except that I don't think that
document.forms.elements.item("text1") works.
document.forms.elements.item(1) works though.
document.forms[0].elements["text1"] should work.
document.form.text1 should work too.
HTH
cudabean
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.