Skip to content

onChange event firing even when value is not changed #88

@ret2jazzy

Description

@ret2jazzy

The onChange event of a select tag gets fired in some cases when the options are altered programmatically. This should not happen as the onChange event should only fire when the user actually changes the value himself.

Here is a tiny example (this took me a lot of time to isolate)

<html>
   <head>
      <script src="static/js/libs/easydropdown/easydropdown.js"></script>
   </head>
   <body>
      <select id="OPT1">
         <option value="E1">E1</option>
      </select>
      <script>
         document.body.onload = ()=> {
             document.getElementById('OPT1').addEventListener('change', function(){
                 alert("Shouldn't be called");
             })
         
             easydropdown.all()

            document.getElementById('OPT1').remove(0) //remove the option

             //add a new option, this will trigger an onChange and an alert will pop up
             let opt = document.createElement("option");
             opt.text = "TEST"
             opt.value = "MODE3";
             document.getElementById('OPT1').add(opt);

         }
 
         
      </script>
   </body>
</html>

Basically, create a select tag with a option, remove the option and then add another option. This leads to the firing of the onChange event. The above snippet will pop an alert() whereas it shouldn't.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions