Results 1 to 5 of 5

Thread: Javascript, how to get value of combobox

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2003
    Posts
    784

    Question Javascript, how to get value of combobox

    Dear All,

    I am learning javascript and now trying to get the value of selected combo box using below javascript.. but it does not work.. could any body please help?


    function JSGetSelectedItem() {
    alert("Hello JSCript " + document.form1.select.options(document.form1.select.selectedindex).text);
    }



    ===

    and I call it from my PHP codes..

    <select name="select" size="1" onchange="JSGetSelectedItem()">
    <?php
    for ($i=1;$i<=10;$i++)
    {
    echo "<option>".$i;
    }
    ?>
    </select>

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Javascript, how to get value of combobox

    You've no closing </option> tags.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2003
    Posts
    784

    Re: Javascript, how to get value of combobox

    Yes.. I knew .. I just want to make it simple ..

    I tried to change it to

    echo "<option>".$i."</option>";

    but still unable to get the value of the combo box.. it always return "Hello JScript 1"

    any idea?

    Thanks & Regards
    Winanjaya

  4. #4
    Hyperactive Member coothead's Avatar
    Join Date
    Oct 2007
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    285

    Re: Javascript, how to get value of combobox

    Hi there Winanjaya,

    have a look at this example it may, hopefully , answer your problem...
    Code:
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
       "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    
    <script type="text/javascript">
    
    window.onload=function() {
    
    var df=document.forms[0][0]; /* this is another method for accessing form elements*/
    
    df.onchange=function() {
    
       val=df.options[df.selectedIndex].value;
    
       txt=df.options[df.selectedIndex].text;
    
       alert('without a set "option value", \nOpera and Firefox take...\n\n "value" to refer to the "option text"'+
             ' ...\n\n '+val+'\n\n...IE returns nothing,\n\n but will submit the "text" as a "value"');
    
       alert('IE will only respond to "text" to give "option text"...\n\n '+txt);
      }
     }
    </script>
    
    </head>
    <body>
    
    <form action="http://www.google.com/" method="get"> <!--google is good for testing form submissions-->
    <div>
    <select name="select" >
    <option>option one</option>
    <option>option two</option>
    <option>option three</option>
    <option>option four</option>
    <option>option five</option>
    </select>
    <input type="submit">
    </div>
    </form>
    
    </body>
    </html>
    
    coothead

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2003
    Posts
    784

    Talking Re: Javascript, how to get value of combobox

    Hello,

    just want to share..

    this codes below solved my problem..

    any way .. thanks a lot!

    Regards
    Winanjaya


    function JSGetSelectedItem() {
    var dropdownIndex = document.getElementById('select').selectedIndex;
    var dropdownValue = document.getElementById('select')[dropdownIndex].text;
    alert("Hello JSCript " + dropdownValue);
    }

Posting Permissions

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



Click Here to Expand Forum to Full Width