How can I create an editable dropdownlist in HTML?

前端 未结 11 1081
终归单人心
终归单人心 2020-11-29 01:51

I\'d like to create a text field with a dropdown list that lets the user choose some predefined values. The user should also be able to type a new value or select a predefin

11条回答
  •  -上瘾入骨i
    2020-11-29 02:29

    Very simple implementation (only basic functionality) based on CSS and one line of JavaScript code.

    .dropdown {
      position: relative;
      width: 200px;
    }
    
    .dropdown select {
      width: 100%;
    }
    
    .dropdown > * {
      box-sizing: border-box;
      height: 1.5em;
    }
    
    .dropdown input {
      position: absolute;
      width: calc(100% - 20px);
    }

    Please note: it uses previousElementSibling() which is not supported in older browsers (below IE9)

提交回复
热议问题