Advertisement
collinsanele

onchange event

May 21st, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <html>
  2. <head>
  3.   <style>
  4.     #div1, #div2{
  5.       display:none;
  6.     }
  7.   </style>
  8. </head>
  9. <body>
  10.   <h2>An example of onchange event</h2>
  11.   <div id="div1">
  12.     <strong> I can be visible and invisible</strong>
  13.   </div>
  14.   <br>
  15.   <div id="div2">
  16.     <strong> I am the second text</strong>
  17.   </div>
  18.   <br>
  19.   <select id="dropdown">
  20.     <option value="one">One</option>
  21.     <option value="two">Two</option>
  22.   </select>
  23.  
  24.   <script>
  25.     let select = document.querySelector("#dropdown");
  26.     select.addEventListener("change", (e)=>{
  27.      if (e.target.value==="one"){
  28.        document.getElementById("div1").style.display = "block";
  29.        document.getElementById("div2").style.display = "none";
  30.      }
  31.      else if(e.target.value==="two"){
  32.        document.getElementById("div2").style.display = "block";
  33.        document.getElementById("div1").style.display = "none";
  34.      }
  35.     });
  36.   </script>
  37. </body>
  38. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement