...

View Full Version : using a variable in a scope chain



munchen
08-17-2004, 11:54 PM
Hi, I have a form that allows users to optionally answer a dozen or so questions while other fields are mandatory. I have finished the code for the mandatory fields. Now, I want several optional fields to access the same function where they will be given value. I am trying to do this using a variable with a function call triggered by the Onblur property. I think this will work if I can do something like this:
document.form.Myform.<form element>.value
where <form element> is an optional text input element in the form denoted by a variable in the Onblur property. The actual scope chain looks like this:
var option
document.form.Myform.option.value

I am having no luck in finding references to whether it is permissable in JS to do this and how? suggestions, tip, clues, etc. greatly appreciated.
Munchen

P.S.
I have been less than clear about this so let me try to summarize: what I have been trying to ask is whether it is possible (syntactically) to use a variable in a scope chain. Now, I am not sure 'scope chain' is the correct phrase but I found it on a javascript tutorial site. What I mean by this is a series of objects separated by the dot (.) operator, e.g., "document.forms.Myform.element.value". Now, if that is clear, can I use a variable in the position where, say, the form elements go since I would like several fields to take on the same value IF the user chooses not to complete those fields on the form.

jamescover
08-18-2004, 02:33 AM
I want several optional fields to access the same function where they will be given value. I am trying to do this using a variable with a function call triggered by the Onblur property.



document.form.Myform.option.value


This should be forms.

Here is the proper syntax:



<script type="text/javascript">
<!--

function chkFrm(){
var doc = document['oFrm']['oTxt'].value;
alert (doc);
}

//-->
</script>

<form name="oFrm">
<input name="oTxt" type="text" size="12" />
<input name="oBtn" type="button" value="Check Form" onclick="javascript:chkFrm();" />
</form>


am having no luck in finding references to whether it is permissable in JS to do this and how?

Do what? onblur is a valid text form field handler. I think you'll need a better explanation, along with some sample code, to warrant a feasible solution.




-james

Kor
08-18-2004, 11:51 AM
As forms and forms' elements are arrays you may use

document[form_name_or_index][element_name_or_index]
or
document.forms[form_name_or_index][element_name_or_index]
or
document.forms[form_name_or_index].elements[element_name_or_index]

if name, use quotes as it is a string. So you may use parameters or whichever to refere elements in an incremental way (or through a loop)

munchen
08-18-2004, 06:01 PM
Thanks for the help. I have added a postscript to my post since I am still not sure about the syntax. However, from reading your replies I have learned a few things about javascript, and, also, about posting, for which I offer many thanks

Munchen

glenngv
08-19-2004, 06:20 AM
See my sig for more info of the Square Bracket Notation which Kor used.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum