Qn from Novice

Discussion in 'Javascript' started by peter, Dec 3, 2004.

  1. peter

    peter Guest

    (i'm new to javascript) I found the following Javascript and is confused by
    its syntax. Why the expression "window.status=input.form[0].value;" is
    valid (i verified this in ie6) and has the same result as the expression
    "window.status=input.form.elements[0].value"?

    As fas as I understand, "input.form" represents the form object. And, this
    form object isnt an array. Why we can use array accessing syntax on an
    ordinary object?

    Any expert can help? thx
    ============================================================================
    =====

    <html><HEAD>
    <!-- **************************************************************** -->
    <SCRIPT LANGUAGE="JavaScript">

    function displayFormValue(input) {
    window.status=input.form[0].value;
    // why this syntax can be used instead of "input.form.elements[0].value"
    return true;
    }
    </script>
    <!-- **************************************************************** -->
    </HEAD><body>
    <form name=form1>
    <input name=text1 type=text onKeyUp="return displayFormValue(this);"
    size=10/>

    </form>
    <br>good to see you
    </body>
    </html>
    peter, Dec 3, 2004
    #1
    1. Advertising

  2. peter

    VK Guest

    There is not such array as form(), but there is forms() array:
    document.forms.elements[j]

    displayFormValue(this) gives you a reference on the input element itself, so
    in your function you just say:
    window.status=input.value;
    which is the right syntacs

    Any of other syntacs work in IE only because this browser has been built
    with a very high tolerance to bad syntacs.
    As long as IE can get any rough idea what in the name are trying to do, it
    will re-adjust your code internally in some more descent way, and it will
    execute the result.
    Sometimes it's good, sometimes it's bad (first of all, no guarantee that IE
    will decrypt your intentions properly; secondly, it may produce well-hidden
    sporadic bugs).
    VK, Dec 3, 2004
    #2
    1. Advertising

  3. peter wrote:

    > (i'm new to javascript) I found the following Javascript and is confused by
    > its syntax. Why the expression "window.status=input.form[0].value;" is
    > valid (i verified this in ie6) and has the same result as the expression
    > "window.status=input.form.elements[0].value"?
    >
    > As fas as I understand, "input.form" represents the form object. And, this
    > form object isnt an array. Why we can use array accessing syntax on an
    > ordinary object?


    Well what you think is "array accessing syntax" is simply a way to
    access properties of an object in JavaScript, see
    http://www.jibbering.com/faq/faq_notes/square_brackets.html

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Dec 3, 2004
    #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. ra

    VHDL novice question

    ra, Jul 13, 2004, in forum: VHDL
    Replies:
    6
    Views:
    2,817
    Allan Herriman
    Jul 13, 2004
  2. jbl
    Replies:
    2
    Views:
    463
    Joe Smith
    Dec 16, 2004
  3. sean
    Replies:
    1
    Views:
    573
    Cowboy \(Gregory A. Beamer\)
    Oct 20, 2003
  4. sean
    Replies:
    2
    Views:
    2,369
  5. sean
    Replies:
    0
    Views:
    301
Loading...

Share This Page