How do I get the change event for a datalist?

前端 未结 6 753
轮回少年
轮回少年 2021-02-05 11:14

I am using a datalist and need to detect when the user selects something from the drop-down list. A similar question has been asked BUT I need it so that the event fires ONLY wh

6条回答
  •  甜味超标
    2021-02-05 12:06

    In browser with the inputType property on the InputEvent you can use that to filter out any unwanted onInput events. This is "insertReplacementText" on Firefox 81 and null for Chrome/Edge 86. If you need to support IE11 you will need to validate the value is valid.

    document.getElementById("browser")
      .addEventListener("input", function(event){
            if(event.inputType == "insertReplacementText" || event.inputType == null) {
              document.getElementById("output").textContent =  event.target.value;
              event.target.value = "";
        }
    })
    
    
    
      
    

提交回复
热议问题