Advertisement
1xptolevitico69

SELECT option onchange

Jan 21st, 2022
1,923
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 1.33 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <title>SELECT option onchange</title>
  4. <style>
  5.   #fruits {
  6.     width: 350px;
  7.     text-align: center;
  8.   }
  9.  
  10.   select {
  11.     color: red;
  12.     font-size: 40px;
  13.     letter-spacing: 5px;
  14.   }
  15.  
  16.   #txt {
  17.     text-transform: capitalize;
  18.   }
  19.  
  20.   button:nth-child(1) {
  21.     margin: 5px;
  22.     float: right;
  23.   }
  24.  
  25.   .buttonContainer {
  26.     width: 100px;
  27.     float: right;
  28.   }
  29. </style>
  30.  
  31. <body>
  32.  
  33.   <select name="fruits" id="fruits">
  34.     <option disabled selected value>Select</option>
  35.     <option value="orange">Orange</option>
  36.     <option value="banana">Banana</option>
  37.     <option value="carrot">Carrot</option>
  38.     <option value="watermelon">Watermelon</option>
  39.     <option value="lime">Lime</option>
  40.   </select>
  41.  
  42.   <div class='buttonContainer'>
  43.     <button onclick='getOptions()'>Get options</button>
  44.     <button onclick='clearOptions()'>Clear options</button>
  45.   </div>
  46.  
  47.   <p id='txt'></p>
  48.   <script>
  49.     x = [];
  50.     frut = document.getElementsByName('fruits');
  51.     window.addEventListener('change', function() {
  52.       for (i = 0; i < frut.length; i++) {
  53.        x.push(frut[i].value);
  54.        x.sort();
  55.      }
  56.    });
  57.  
  58.    function getOptions() {
  59.      txt.innerHTML = x;
  60.    }
  61.  
  62.    function clearOptions() {
  63.      txt.innerHTML = '';
  64.      return x = [];
  65.    }
  66.  </script>
  67. </body>
  68.  
  69. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement