How to retrieve "option value" info from select tag

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cepera
    New Member
    • Aug 2008
    • 8

    #1

    How to retrieve "option value" info from select tag

    Hi guys!
    How I can retrieve "option value" info from select tag?
    I am using this code, but it is only let me get "name".
    I need to pass this value to second select tag.
    I found this code here:

    but they do not have any instructions how to retrieve option values.

    ----------JS------------

    [HTML]<script>
    function handleOnChange( dd1)
    {
    var idx = dd1.selectedInd ex;
    var val = dd1[idx].text;
    var par = document.forms["frmSelect"];
    var parelmts = par.elements;
    var prezsel = parelmts["prez"];
    var course = val;
    if (course != "-Select a Course-")
    {
    Http.get({
    url: "./" + course + ".txt",
    callback: fillPrez,
    cache: Http.Cache.Get
    }, [prezsel]);
    }
    }

    function fillPrez(xmlrep ly, prezelmt)
    {
    if (xmlreply.statu s == Http.Status.OK)
    {
    var prezresponse = xmlreply.respon seText;
    var prezar = prezresponse.sp lit("|");
    prezelmt.length = 1;
    prezelmt.length = prezar.length;
    for (o=1; o < prezar.length; o++)
    {
    prezelmt[o].text = prezar[o];
    }
    }
    else
    {
    alert("Cannot handle the AJAX call.");
    }
    }
    </script>[/HTML]

    ------HTML----------

    [HTML]<form name="courseSel ect">

    <select name="course" onChange.value= "handleOnChange (this);">

    <option value='0'>-Select a Course-</option>
    <option value='1'>Cours e Name 1</option>
    <option value='2'>Cours e Name 2</option>
    <option value='3'>Cours e Name 3</option>

    </select>

    </form>
    [/HTML]

    Thank you

    Sergey.
    Last edited by gits; Aug 6 '08, 07:10 AM. Reason: added code tags
  • samikhan83
    New Member
    • Sep 2007
    • 33

    #2
    hi..
    1. you have to first change onchange.value TO only onchange.
    2. U can get the value of th select field easily by using document.getEle mentById.value. ...

    Comment

    • cepera
      New Member
      • Aug 2008
      • 8

      #3
      Thank you samikhan83.
      I am newbyte in JS & AJAX
      Can you please post some example?

      Comment

      • RamananKalirajan
        Contributor
        • Mar 2008
        • 608

        #4
        Hi Dude, hope this code may help you. If u have any doubts post it I will surely help u out.

        [HTML]<html>
        <head>
        <script language="javas cript">
        function doThis()
        {
        var sel = document.getEle mentById('mySel ect1').selected Index;
        var selObj = document.getEle mentById('mySel ect1').options[sel].text;
        document.getEle mentById('mySel ect2').add(new Option(selObj,s elObj));
        document.getEle mentById('mySel ect1').remove(s el);

        }
        </script>
        </head>
        <table>
        <tr>
        <td>
        <select id="mySelect1" size="10" onclick="doThis ()">
        <option>Apple </option>
        <option>Orang e</option>
        <option>Mango </option>
        <option>Grape </option>
        <option>Pine</option>
        </select>
        </td>
        <td>
        <select id="mySelect2"s ize="10" >
        </select>
        </td>
        </tr>
        </table>
        </html>[/HTML]

        Regards
        Ramanan Kalirajan

        Comment

        • acoder
          Recognized Expert MVP
          • Nov 2006
          • 16032

          #5
          Change line 5 to:
          [code=javascript]var val = dd1[idx].value;[/code]or replace lines 4 and 5 with:
          [code=javascript]var val = dd1.value;[/code]

          Comment

          • cepera
            New Member
            • Aug 2008
            • 8

            #6
            Originally posted by acoder
            Change line 5 to:
            [code=javascript]var val = dd1[idx].value;[/code]or replace lines 4 and 5 with:
            [code=javascript]var val = dd1.value;[/code]

            !!! THANK YOU VERY MUCH ACODER !!!

            Comment

            • acoder
              Recognized Expert MVP
              • Nov 2006
              • 16032

              #7
              You're welcome :) Post again if you have more questions.

              Comment

              Working...