"Object does not support.." error with document.getElementById("dropdown")

Discussion in 'Javascript' started by Phil Powell, Nov 25, 2008.

  1. Phil Powell

    Phil Powell Guest

    Consider this code:

    <code>
    <pre>

    <html>
    <head>
    <script type="text/javascript" language="JavaScript">
    <!--

    function verifyForm(docForm) {
    var errMsg = "";
    errMsg = verifyFormElements(docForm);

    if (errMsg != "") {
    alert(errMsg);
    return false;
    }

    return true;
    }

    function verifyFormElements(docForm) {
    var errMsg = "";
    var canUseElementById = (docForm.getElementById);

    if (canUseElementById && docForm.getElementById("lastname").value ==
    "") {
    errMsg += "Please enter your last name\\n\\n";
    }

    if (canUseElementById && docForm.getElementById("firstname").value ==
    "") {
    errMsg += "Please enter your first name\\n\\n";
    }

    var dropdown = docForm.getElementById("category");

    if (canUseElementById && dropdown.options
    [dropdown.selectedIndex].value == "") {
    errMsg += "Please choose a catgory\\n\\n";
    }

    return errMsg;

    }
    //-->
    </script>
    </head>
    <body>
    <form method="post" action="here" onsubmit="return verifyForm(this)">
    <table border="0">
    <tr>
    <td>Last name:</td>
    <td><input id="lastname" name="lastname" size="30"></td>
    </tr>
    <tr>
    <td>First name:</td>
    <td><input id="firstname" name="firstname" size="30"></td>
    </tr>
    <td>Category:</td>
    <td>
    <select id="category" name="category">
    <option value="1">Security</option>
    <option value="2">IT</option>
    <option value="3">Administration</option>
    </select>
    </td>
    </tr>
    <tr>
    <td colspan="2"><input type="submit" name="submit" value="submit"></
    td>
    </tr>
    </table>
    </form>
    </body>
    </html>

    </pre>
    </code>

    When I try to run it in IE6 (only option allowed by me at this time -
    honestly, I am not allowed to install any other browser yet here at
    work!) I get the "Object does not allow this property or method" error
    on the following line:

    var dropdown = docForm.getElementById("category");

    Why does this happen? Is this an IE6 hiccup on using ID elements on a
    dropdown in Javascript or am I missing something else?

    Thanks
     
    Phil Powell, Nov 25, 2008
    #1
    1. Advertising

  2. Phil Powell

    Henry Guest

    On Nov 25, 5:55 pm, Phil Powell wrote:
    <snip>
    > var dropdown = docForm.getElementById("category");
    >
    > Why does this happen?


    The object referred to by - docForm - does not have a - getElementById
    - method (it is a FORM element not a document, despite the odd name).

    > Is this an IE6 hiccup on using ID elements on a
    > dropdown in Javascript


    No, it is a programmer error (or several).

    > or am I missing something else?


    At minimum.
     
    Henry, Nov 25, 2008
    #2
    1. Advertising

  3. Phil Powell

    Phil Powell Guest

    On Nov 25, 1:03 pm, Henry <> wrote:
    > On Nov 25, 5:55 pm, Phil Powell wrote:
    > <snip>
    >
    > > var dropdown = docForm.getElementById("category");

    >
    > > Why does this happen?

    >
    > The object referred to by - docForm - does not have a - getElementById
    > - method (it is a FORM element not a document, despite the odd name).
    >
    > > Is this an IE6 hiccup on using ID elements on a
    > > dropdown in Javascript

    >
    > No, it is a programmer error (or several).
    >
    > > or am I missing something else?

    >
    > At minimum.


    Care to come up with a solution.. at minimum?
     
    Phil Powell, Nov 25, 2008
    #3
  4. Phil Powell meinte:
    > On Nov 25, 1:03 pm, Henry <> wrote:
    >> On Nov 25, 5:55 pm, Phil Powell wrote:
    >> <snip>
    >>
    >>> var dropdown = docForm.getElementById("category");
    >>> Why does this happen?

    >> The object referred to by - docForm - does not have a - getElementById
    >> - method (it is a FORM element not a document, despite the odd name).
    >>
    >>> Is this an IE6 hiccup on using ID elements on a
    >>> dropdown in Javascript

    >> No, it is a programmer error (or several).
    >>
    >>> or am I missing something else?

    >> At minimum.

    >
    > Care to come up with a solution.. at minimum?


    Use document.getElementById(). Now that was easy...

    Anyway, if you want to improve on your JS abilities (or rather get the
    basics right) you should consider to read some documentation first ...
    at minimum. Otherwise the next question will inevitable revolve around
    IE's odd behaviour with ids and names.

    Gregor
     
    Gregor Kofler, Nov 25, 2008
    #4
  5. Phil Powell

    Phil Powell Guest

    On Nov 25, 1:29 pm, Gregor Kofler <> wrote:
    > Phil Powell meinte:
    >
    >
    >
    >
    >
    > > On Nov 25, 1:03 pm, Henry <> wrote:
    > >> On Nov 25, 5:55 pm, Phil Powell wrote:
    > >> <snip>

    >
    > >>> var dropdown = docForm.getElementById("category");
    > >>> Why does this happen?
    > >> The object referred to by - docForm - does not have a - getElementById
    > >> - method (it is a FORM element not a document, despite the odd name).

    >
    > >>> Is this an IE6 hiccup on using ID elements on a
    > >>> dropdown in Javascript
    > >> No, it is a programmer error (or several).

    >
    > >>> or am I missing something else?
    > >> At minimum.

    >
    > > Care to come up with a solution.. at minimum?

    >
    > Use document.getElementById(). Now that was easy...


    That makes no sense. I am passing document.forms[0] to pass the form
    object to get the form ID value; why instead must I use the document
    object?

    >
    > Anyway, if you want to improve on your JS  abilities (or rather get the
    > basics right) you should consider to read some documentation first ...
    > at minimum. Otherwise the next question will inevitable revolve around
    > IE's odd behaviour with ids and names.
    >
    > Gregor- Hide quoted text -
    >
    > - Show quoted text -
     
    Phil Powell, Nov 25, 2008
    #5
  6. Phil Powell meinte:
    > On Nov 25, 1:29 pm, Gregor Kofler <> wrote:


    >> Use document.getElementById(). Now that was easy...

    >
    > That makes no sense. I am passing document.forms[0] to pass the form
    > object to get the form ID value; why instead must I use the document
    > object?


    Sigh... Because forms don't have a getElementById() method. Document has.
     
    Gregor Kofler, Nov 25, 2008
    #6
  7. Phil Powell

    Doug Gunnoe Guest

    On Nov 25, 12:31 pm, Phil Powell <> wrote:

    > That makes no sense.  I am passing document.forms[0] to pass the form
    > object to get the form ID value; why instead must I use the document
    > object?


    Try docForm.id

    1. You already have the node you need, no need to use getElementById()
    2. On Nov 25, 12:56 pm, Gregor Kofler <> wrote:
    > Sigh... Because forms don't have a getElementById() method. Document has.
     
    Doug Gunnoe, Nov 25, 2008
    #7
  8. Doug Gunnoe meinte:
    > On Nov 25, 12:31 pm, Phil Powell <> wrote:
    >
    >> That makes no sense. I am passing document.forms[0] to pass the form
    >> object to get the form ID value; why instead must I use the document
    >> object?

    >
    > Try docForm.id
    >
    > 1. You already have the node you need, no need to use getElementById()


    Nope. That won't help to get references to elements within the form. The
    form id is of no use for him.

    Gregor
     
    Gregor Kofler, Nov 25, 2008
    #8
  9. Phil Powell

    Doug Gunnoe Guest

    On Nov 25, 3:45 pm, Gregor Kofler <> wrote:
    > Nope. That won't help to get references to elements within the form. The
    > form id is of no use for him.
    >
    > Gregor


    If he is passing a node to his function he already has the node's id,
    no need for getElementById().

    But yeah, you're right, he doesn't need it.
     
    Doug Gunnoe, Nov 26, 2008
    #9
    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. Dave L
    Replies:
    3
    Views:
    3,287
    Göran Andersson
    Mar 4, 2010
  2. Ghyslaine Crespe
    Replies:
    4
    Views:
    647
    Evertjan.
    Dec 26, 2004
  3. wk
    Replies:
    5
    Views:
    179
    Dietmar Meier
    Apr 22, 2005
  4. Martin
    Replies:
    3
    Views:
    159
  5. Replies:
    4
    Views:
    173
    Jonas Raoni
    Nov 21, 2006
Loading...

Share This Page