Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Aug 2004
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    using a variable in a scope chain

    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.
    Last edited by munchen; 08-18-2004 at 04:56 PM.

  • #2
    Regular Coder
    Join Date
    Aug 2002
    Location
    USA
    Posts
    625
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:


    Code:
    <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
    "God so loved the world that he gave his only begotten son, so that whosoever believed in him would not perish, but have everlasting life. For God did not send his son into the world to condemn the world, but so that through him the world might be saved. "

  • #3
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    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)
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #4
    New to the CF scene
    Join Date
    Aug 2004
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    thanks

    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

  • #5
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,030
    Thanks
    0
    Thanked 247 Times in 244 Posts
    See my sig for more info of the Square Bracket Notation which Kor used.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •