unable to get value of select tag

Discussion in 'JavaScript' started by progfrog, Jan 9, 2009.

  1. #1
    Hi!
    can anyone tell me what is wrong with the code?

    It has something to do with the way i call the value of the selected option but i cannot track the problem..

    
    
    
    <html>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1255 ">
    <head>
    <script type="javascript">
    function foo(l,c)
    {
    
    var z=document.getElementById(c) 
     alert(l.z.options[l.z.options.selectedIndex].value) 
    
    }
    
    
    
    
    </script>
    
    
    
    
    </head>
    
    <body>
    <form id="ghost">
    <select name="bery" id="sel" onchange="foo('ghost','sel')">
    <option>1</option>
    <option>2</option>
    </select>
    </body>
    
    </form>
    </html>
    Code (markup):
     
    progfrog, Jan 9, 2009 IP
  2. cont911

    cont911 Peon

    Messages:
    50
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    1) use <script type="text/javascript"> instead of <script type="javascript">
    2) add semicolon at the end of each operator
    3) place </form> tag before </body>
    4) use innerHTML to access content of option tags

    <html>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1255 ">
    <head>
    <script type="text/javascript">
    function foo(objName)
    {
    var sel=document.getElementById(objName);
    var index = sel.selectedIndex;
    alert(sel.options[index].innerHTML);
    }
    </script>
    </head>
    <body>
    <form id="ghost">
    <select name="bery" id="sel" onchange="foo('sel')">
    <option>1</option>
    <option>2</option>
    </select>
    </form>
    </body>
    </html>
     
    cont911, Jan 9, 2009 IP
    webrickco likes this.
  3. zelenooq

    zelenooq Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    <html>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1255 ">
    <head>
    <script>
    function foo(l,c){
    alert(document.forms[l].elements[c].value)
    }
    </script>




    </head>

    <body>
    <form name="ghost">
    <select name="sel" onchange="foo('ghost','sel')">
    <option value="1">1</option>
    <option value="2">2</option>
    </select>
    </form>

    </body>
    </html>


    u can put this
    foo(this)
    and in function foo
    function foo(object) {
    alert(this.value)
    }
     
    zelenooq, Jan 10, 2009 IP