www.webdeveloper.com
Results 1 to 4 of 4

Thread: [RESOLVED] Object Expected when Using getElementById on a Select

  1. #1
    Join Date
    Jul 2010
    Location
    Cobalt, Ontario, Canada
    Posts
    15

    resolved [RESOLVED] Object Expected when Using getElementById on a Select

    This code generates an "objected expected" error when I select the unselected option in the list.

    Code:
    <html><head>
    <script type="text/javascript">
    function wgxx() {
        var temp = getElementById ("zzzz");
    }
    </script>
    </head><body>
    <form action="abcd.php">
        <select id="zzzz" onChange="wgxx()">
        <option>1</option>
        <option>2</option>
        </select>
    </form>
    </body></html>
    Does anyone have any idea why this is happening?

  2. #2
    Join Date
    Jan 2004
    Location
    chertsey, a small town s.w. of london, england.
    Posts
    1,456
    Hi there WarrenGaebel,

    does this help...
    Code:
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
       "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
    <head>
    
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <meta name="language" content="english"> 
    <meta http-equiv="Content-Style-Type" content="text/css">
    <meta http-equiv="Content-Script-Type" content="text/javascript">
    
    <title></title>
    
    <style type="text/css"></style>
    
    <script type="text/javascript">
    
    function init(){
    document.getElementById('zzzz').onchange=function(){
       wgxx();
      }
     }
    
    function wgxx() {
        var temp=document.getElementById('zzzz');
        alert(temp);
        alert(temp.tagName);
     }
    
    if(window.addEventListener){
       window.addEventListener('load',init,false);
     }
    else { 
    if(window.attachEvent){
       window.attachEvent('onload',init);
      }
     }
    </script>
    
    </head>
    <body>
    
    <form action="abcd.php">
    <div>
    <select id="zzzz">
     <option value="1">1</option>
     <option value="2">2</option>
    </select>
    </div>
    </form>
    
    </body>
    </html>
    
    coothead

  3. #3
    Join Date
    Jul 2010
    Location
    Cobalt, Ontario, Canada
    Posts
    15

    Thank-you

    Thank-you for your solution. I did not put "document." in front of "getElementById". The following works fine:

    Code:
    <html><head>
    <script type="text/javascript">
    function wgxx() {
        var temp = document.getElementById ("zzzz");
    }
    </script>
    </head><body>
    <form action="abcd.php">
        <select id="zzzz" onChange="wgxx()">
        <option>1</option>
        <option>2</option>
        </select>
    </form>
    </body></html>

  4. #4
    Join Date
    Jan 2004
    Location
    chertsey, a small town s.w. of london, england.
    Posts
    1,456
    No problem you're welcome.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles