Wednesday, August 28, 2019

[JavaScript] Dynamic create javaScript event


<div>
  <select id="menu">
    <option>A</option>
    <option>B</option>
    <option>C</option>
  </select>
</div>
<div>
  <select id="menu2">
    <option>D</option>
    <option>E</option>
    <option>F</option>
  </select>
</div>
<script>
window.onload = function(){
  // Method 1
  var menuObj = document.getElementById("menu");
  menuObj.onchange = function(){
    alert("This is the event 1 created dynamically.");
  }
  // Method 2
  var menuObj2 = document.getElementById("menu2");
  menuObj2.addEventListener("change", function(){
    alert("This is the event 2 created dynamically.");
  });
}
</script>
Reference :
https://blog.csdn.net/lxf512666/article/details/52878621

No comments :

Post a Comment