Find previously selected combo box value in javascript

HTML form:

            Here i have taken separate hidden text  box hide the value when we first load the  page, So  then we  easily  find the  solution using the  bellow  javascript  function.



<form name="frm_country" id="frm_country" method="post">
  <select name="country1" id="country1" onchange="CountrySelected();">
     <option value="US" selected="selected">United States</option>
     <option value="UK">United Kingdom</option>
     <option value="IN">India</option>
     <option value="CH">China</option>
  </select>
  <input  type="hidden" name="country_hide" id="country_hide" value= "US">  <!-- DEFAULT US -->
</form>

         Bellow  you can the  javascript  code  to  find the  previously  selected combo box value.


  <script type="text/javascript">

       function  CountrySelected()
       {
           var pre_country_code =  document.getElementById('country_hide').value;
          //here we will  get the value      
          alert(pre_country_code);

          var cur_country_idx = document.frm_country.country1.selectedIndex;
          var cur_combo_val =   document.frm_country.country1.options[cur_country_idx].value;
          document.getElementById('country_hide').value = cur_combo_val; 
       }

  </script>

No comments:

Post a Comment