Search code examples
htmlhtml-selecthtml-input

Drop Down Menu/Text Field in one


I'm working on building new site, and I need a drop down menu to select the amount of something in my site. But at the same time I need this drop down list to accept text. So if the client wants to choose from the drop down list then he can, also if the client want to enter the amount by text then he can also. As you can see I want to make it dual.

For example: suppose there is an amount drop down menu, and its elements are (1,2,3);

Suppose now that the client needs the amount to be 5 - this is his right - it does not exist in the drop down list, so the client now must enter the amount textually. So for any entry the client must either choose from the drop down list or enter the amount textually.

After the description of my problem, and the simple example that I have introduced to you, here is my question:

Is there HTML code to make a drop down menu and a text field in one, as dual, not separated?


Solution

  • Option 1

    Include the script from dhtmlgoodies and initialize like this:

    <input type="text" name="myText" value="Norway"
           selectBoxOptions="Canada;Denmark;Finland;Germany;Mexico"> 
    
    createEditableSelect(document.forms[0].myText);
    

    Option 2

    Here's a custom solution which combines a <select> element and <input> element, styles them, and toggles back and forth via JavaScript

    <div style="position:relative;width:200px;height:25px;border:0;padding:0;margin:0;">
      <select style="position:absolute;top:0px;left:0px;width:200px; height:25px;line-height:20px;margin:0;padding:0;"
              onchange="document.getElementById('displayValue').value=this.options[this.selectedIndex].text; document.getElementById('idValue').value=this.options[this.selectedIndex].value;">
        <option></option>
        <option value="one">one</option>
        <option value="two">two</option>
        <option value="three">three</option>
      </select>
      <input type="text" name="displayValue" id="displayValue" 
             placeholder="add/select a value" onfocus="this.select()"
             style="position:absolute;top:0px;left:0px;width:183px;width:180px\9;#width:180px;height:23px; height:21px\9;#height:18px;border:1px solid #556;"  >
      <input name="idValue" id="idValue" type="hidden">
    </div>