Having trouble understanding function scope and variable scope

Discussion in 'Javascript' started by Andrew Falanga, Nov 22, 2008.

  1. Hello again,

    I'm having trouble understanding why the element types in the form I'm
    using aren't defined in my function. Below is the code (it's a
    nonsense page that I'm using just to get my understanding correct).
    In the function displayFormElements(), the form.type property isn't
    defined, but yet the form.length was because the for loop only goes
    for 3 iterations. Is there something wrong with how I'm passing the
    form to the function?

    Thanks for any help,
    Andy


    <html>
    <head>
    <script type="text/javascript">
    function foo() {
    document.write("foo");
    alert("foo");
    }

    function displayFormElements(form) {
    document.write("displayFormElements called");
    var length = form.length;
    for(var i = 0; i < length; i++) {
    document.write("element " + i + " is " + form.type + "<BR>");
    }
    }

    function CheckForNull() {
    alert("CheckForNull called");
    var formId=document.getElementById("myForm");
    if(formId.elements[0].value == "") {
    alert("form may not be null");
    return false;
    }
    return true;
    }

    </script>
    </head>
    <body>

    Form test
    <form id="myForm" onsubmit="return CheckForNull()" action="index.html"
    method="post">
    <input type=text name="text1"></input>
    <textarea>example text</textarea>
    <input type="submit" value="submit" />
    </form>
    <P>
    <script language="javascript" type="text/javascript">
    var theform = document.getElementById("myForm");
    displayFormElements(theform);
    document.write("<BR>");
    for(var i = 0; i < theform.length; i++) {
    document.write("element " + i + " is type: " + theform.elements
    .type + "<BR>");
    }

    </script>
    </body>
    </html>
    Andrew Falanga, Nov 22, 2008
    #1
    1. Advertising

  2. Andrew Falanga

    David Mark Guest

    On Nov 22, 12:31 pm, Andrew Falanga <> wrote:
    > Hello again,
    >
    > I'm having trouble understanding why the element types in the form I'm
    > using aren't defined in my function.  Below is the code (it's a
    > nonsense page that I'm using just to get my understanding correct).
    > In the function displayFormElements(), the form.type property isn't
    > defined, but yet the form.length was because the for loop only goes
    > for 3 iterations.  Is there something wrong with how I'm passing the
    > form to the function?
    >
    > Thanks for any help,
    > Andy
    >


    No doctype? Why test in quirks mode?

    > <html>
    > <head>
    > <script type="text/javascript">
    > function foo() {
    >    document.write("foo");
    >    alert("foo");
    >
    > }
    >
    > function displayFormElements(form) {
    >    document.write("displayFormElements called");
    >    var length = form.length;


    var length = form.elements.length;

    >    for(var i = 0; i < length; i++) {
    >       document.write("element " + i + " is " + form.type + "<BR>");


    Well, this is just nonsense. What do you expect the "type" property
    of the form to be?

    >    }
    >
    > }
    >
    > function CheckForNull() {
    >    alert("CheckForNull called");
    >    var formId=document.getElementById("myForm");


    var formId = window.document.forms.myForm;
    // TODO: change variable name to something appropriate

    >    if(formId.elements[0].value == "") {
    >       alert("form may not be null");
    >       return false;
    >    }


    What does that mean?

    >    return true;
    >
    > }
    >


    [snip]
    David Mark, Nov 22, 2008
    #2
    1. Advertising

  3. On Nov 22, 12:43 pm, David Mark <> wrote:
    > On Nov 22, 12:31 pm, Andrew Falanga <> wrote:
    >
    > > Hello again,

    >
    > > I'm having trouble understanding why the element types in the form I'm
    > > using aren't defined in my function.  Below is the code (it's a
    > > nonsense page that I'm using just to get my understanding correct).
    > > In the function displayFormElements(), the form.type property isn't
    > > defined, but yet the form.length was because the for loop only goes
    > > for 3 iterations.  Is there something wrong with how I'm passing the
    > > form to the function?

    >
    > > Thanks for any help,
    > > Andy

    >
    > No doctype?  Why test in quirks mode?


    You know, I've been to a couple of different tutorials for javascript
    online and every code example I see simply has what's down there. My
    "actual" code files start with this:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/
    TR/html4/strict.dtd">


    >
    > > <html>
    > > <head>
    > > <script type="text/javascript">
    > > function foo() {
    > >    document.write("foo");
    > >    alert("foo");

    >
    > > }

    >
    > > function displayFormElements(form) {
    > >    document.write("displayFormElements called");
    > >    var length = form.length;

    >
    > var length = form.elements.length;
    >
    > >    for(var i = 0; i < length; i++) {
    > >       document.write("element " + i + " is " + form.type + "<BR>");

    >
    > Well, this is just nonsense.  What do you expect the "type" property
    > of the form to be?
    >


    LOL!! :) It's so annoying when I make simple mistakes like that. I
    didn't notice that, but that was the problem.

    Andy
    Andrew Falanga, Nov 22, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Pyenos
    Replies:
    9
    Views:
    279
    WaterWalk
    Dec 29, 2006
  2. Krumble Bunk

    Having trouble understanding these bitwise macros

    Krumble Bunk, Oct 12, 2005, in forum: C Programming
    Replies:
    5
    Views:
    446
    Skarmander
    Oct 12, 2005
  3. Replies:
    7
    Views:
    296
    Triple-DES
    Mar 14, 2008
  4. Andy Pipes
    Replies:
    6
    Views:
    120
    Andy Pipes
    Sep 30, 2008
  5. Jake Barnes
    Replies:
    3
    Views:
    103
    Thomas 'PointedEars' Lahn
    May 24, 2006
Loading...

Share This Page