Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. how to pass the slected option from a html drop down list to a java script variable??

how to pass the slected option from a html drop down list to a java script variable??

Scheduled Pinned Locked Moved Web Development
javahtmltoolstutorialquestion
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    krrish chaitanya
    wrote on last edited by
    #1

    This is my drop down list..If i select Others here..i need to dispalya text box..and pass that text box value to a java script variabble

    <select id="dept">
                    <option value="Dept1">Dept1</option>
                    <option value="Dept2">Dept2</option>
                    <option value="Dept3">Dept3</option>
                    <option value="Others">Others</option>
                </select>
    
    J C 2 Replies Last reply
    0
    • K krrish chaitanya

      This is my drop down list..If i select Others here..i need to dispalya text box..and pass that text box value to a java script variabble

      <select id="dept">
                      <option value="Dept1">Dept1</option>
                      <option value="Dept2">Dept2</option>
                      <option value="Dept3">Dept3</option>
                      <option value="Others">Others</option>
                  </select>
      
      J Offline
      J Offline
      Jules VDV
      wrote on last edited by
      #2

      If what you want to get is the value attribute then:

      function getValue(){
      var e = document.getElementById("dept");
      var strDept = e.options[e.selectedIndex].value;
      }

      If you want to get the text of the selected value

      function getText(){
      var e = document.getElementById("dept");
      var strDept= e.options[e.selectedIndex].text;
      }

      Then you will need to hook the function into the onchange event of your ...

      Greenridge Business Systems

      C 1 Reply Last reply
      0
      • K krrish chaitanya

        This is my drop down list..If i select Others here..i need to dispalya text box..and pass that text box value to a java script variabble

        <select id="dept">
                        <option value="Dept1">Dept1</option>
                        <option value="Dept2">Dept2</option>
                        <option value="Dept3">Dept3</option>
                        <option value="Others">Others</option>
                    </select>
        
        C Offline
        C Offline
        cjoki
        wrote on last edited by
        #3

        Your select will need a onchange event to call a javascript function that will test the value of the selected option. If it turns out to be equal to "Others" then you can either, add the need html dynamically or just make it visible by using the css display property. javascript

        function test_selection()
        {
        var sel = document.getElementById('dept');
        var val = sel.options[sel.selectedIndex].value;
        if(val=="Others")
        {
        ....you can figure the rest from here.
        }
        }

        html

        <select id="dept" onchange="test_selection();">
        <option value="Dept1">Dept1</option>
        <option value="Dept2">Dept2</option>
        <option value="Dept3">Dept3</option>
        <option value="Others">Others</option>
        </select>

        K 1 Reply Last reply
        0
        • J Jules VDV

          If what you want to get is the value attribute then:

          function getValue(){
          var e = document.getElementById("dept");
          var strDept = e.options[e.selectedIndex].value;
          }

          If you want to get the text of the selected value

          function getText(){
          var e = document.getElementById("dept");
          var strDept= e.options[e.selectedIndex].text;
          }

          Then you will need to hook the function into the onchange event of your ...

          Greenridge Business Systems

          C Offline
          C Offline
          cjoki
          wrote on last edited by
          #4

          HA! tied!

          J 1 Reply Last reply
          0
          • C cjoki

            HA! tied!

            J Offline
            J Offline
            Jules VDV
            wrote on last edited by
            #5

            :-D

            Greenridge Business Systems

            K 1 Reply Last reply
            0
            • J Jules VDV

              :-D

              Greenridge Business Systems

              K Offline
              K Offline
              krrish chaitanya
              wrote on last edited by
              #6

              Thanx a lot Jules..

              1 Reply Last reply
              0
              • C cjoki

                Your select will need a onchange event to call a javascript function that will test the value of the selected option. If it turns out to be equal to "Others" then you can either, add the need html dynamically or just make it visible by using the css display property. javascript

                function test_selection()
                {
                var sel = document.getElementById('dept');
                var val = sel.options[sel.selectedIndex].value;
                if(val=="Others")
                {
                ....you can figure the rest from here.
                }
                }

                html

                <select id="dept" onchange="test_selection();">
                <option value="Dept1">Dept1</option>
                <option value="Dept2">Dept2</option>
                <option value="Dept3">Dept3</option>
                <option value="Others">Others</option>
                </select>

                K Offline
                K Offline
                krrish chaitanya
                wrote on last edited by
                #7

                Thanx a lot..its working..

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups