How To Control Input Type Dropdown Select Options Menu With Jquery

How To Control Input Type Dropdown Select Options Menu With Jquery

I am creating a web site. In this web site , there is a form. I have added 3 dropdown menus in this form like below. Now I want to when someone selects the number 5 from Adults menu , I want to show number 5 in the infants menu. And also I want t keep Adults + Children = 9. Then if this Total is below than 9 , I want to show that extra number in the infants menu. I mean like this ,


Adults + Children = 9
Adults = 5 and infants = 5
But , Adults + Children = 8
Adults = 5 , Children = 3 , it means there is still 1. 
Now I want to show that 1 with infants , 1 + 5 = 6.

Here is the Jquery part.

  1. $(document).ready(function() {

  2.   var adults = parseFloat($('#adults').val());
  3.   var children = parseFloat($('#children').val());
  4.   var infants = $('#infants').val();
  5.   var Total = adults + children;
  6.   //$("#errorModal").html(Total);

  7.   var totalAdultChild = 9;
  8.   $('#adults').change(function() {
  9.     var adultValue = this.value;
  10.     if (this.value > 8) {
  11.       $("#children").prop('disabled', true);

  12.     } else {
  13.       $("#children").prop('disabled', false);

  14.       $('#children option').each(function(index, element) {
  15.         if ((totalAdultChild - adultValue) < this.value)
  16.           $(this).hide();
  17.         else
  18.           $(this).show();
  19.       });

  20.       $('#infants option').each(function(index, element) {
  21.         if (adultValue < this.value)
  22.           $(this).hide();
  23.         else
  24.           $(this).show();
  25.       });

  26.     }
  27.   });


  28.   //alert(Total);

  29. });

Here is HTML Dropdown View.

  1. <form class="form-horizontal" method="POST" action="#" enctype="multipart/form-data" id="signupForm">

  2.   <div class="col-md-4 col-sm-12 hero-feature">
  3.     <!-- Start Of The Col Class -->
  4.     Adults :
  5.     <select name="adults" class="form-control" id="adults">
  6.       <option value="1">1</option>
  7.       <option value="2">2</option>
  8.       <option value="3">3</option>
  9.       <option value="4">4</option>
  10.       <option value="5">5</option>
  11.       <option value="6">6</option>
  12.       <option value="7">7</option>
  13.       <option value="8">8</option>
  14.       <option value="9">9</option>
  15.     </select> <br>
  16.   </div>

  17.   <div class="col-md-4 col-sm-12 hero-feature">
  18.     <!-- Start Of The Col Class -->
  19.     Children :
  20.     <select name="children" class="form-control" id="children">
  21.       <option value="1">1</option>
  22.       <option value="2">2</option>
  23.       <option value="3">3</option>
  24.       <option value="4">4</option>
  25.       <option value="5">5</option>
  26.       <option value="6">6</option>
  27.       <option value="7">7</option>
  28.       <option value="8">8</option>
  29.       <option value="9">9</option>
  30.     </select> <br>

  31.   </div>

  32. <div class="col-md-4 col-sm-12 hero-feature">
  33. <!-- Start Of The Col Class -->
  34. Children :
  35. <select name="infants" class="form-control" id="infants">
  36.   <option value="1">1</option>
  37.   <option value="2">2</option>
  38.   <option value="3">3</option>
  39.   <option value="4">4</option>
  40.   <option value="5">5</option>
  41.   <option value="6">6</option>
  42.   <option value="7">7</option>
  43.   <option value="8">8</option>
  44.   <option value="9">9</option>
  45. </select> <br>

  46.   </div>

  47.   <a href="#" id="ghsubmitbtn" class="btn btn-success">Search Flight Data</a>

  48. </form>