-6

I have this piece of code: input type="text" name="email" id="email" value="" placeholder="" class="txt"

This is a simple input.

What I need. Click on this input type="text" -> a drop-down select menu with several options appears (but it is still possible to write something manually into this input type="text") -> click on any option -> one of the options is inserted into the input type="text" -> click again on the input type="text" -> the same drop-down select menu with several options appears.

Please help to do this.

williamzo
  • 7
  • 5
  • 1
    Possible duplicate of [HTML select form with option to enter custom value](https://stackoverflow.com/questions/5650457/html-select-form-with-option-to-enter-custom-value) – MisterMystery Feb 08 '18 at 21:51
  • There is no a good answer on that page according to what I need. – williamzo Feb 08 '18 at 21:56

2 Answers2

1

This can't be done with the standard form controls alone, but you can make your own. See the comments below for explanation.

// Get references to elements used
var input = document.getElementById("selectedBrowser");
var list = document.getElementById("list");

// Get all the list items into an array
var listItems = Array.prototype.slice.call(document.querySelectorAll("#list > div"));

// Make the "drop down" list the same width as the input
list.style.width = getComputedStyle(input).width;

// Set up click handler on the input
input.addEventListener("click", function(){ 
 list.classList.remove("hidden");  // Show the list
});

// Set up input event handler on input
input.addEventListener("input", function(){ 
 list.classList.add("hidden");  // Hide the list
});


// Loop over the list items
listItems.forEach(function(item){
  // Set up a click event handler
  item.addEventListener("click", function(){
    input.value = item.textContent;    // Copy the value into the input
   list.classList.add("hidden");       // Hide the list
  });

  // Set up a mouseover event handler
  item.addEventListener("mouseover", function(){
    item.classList.add("highlight");       // Hide the list
  });
  
  // Set up a mouseout event handler  
  item.addEventListener("mouseout", function(){
    item.classList.remove("highlight");       // Hide the list
  });
});
/* Applied to the drop down list by default to hide it and
   removed when the list needs to be shown. */
.hidden {
 display:none;
}

#container {
  display:inline-block;
  vertical-align:top;
}

/* Ensures that the input will be positioned at the top-left of its parent */
#selectedBrowser {
  position:absolute;
}

#list {
  position:absolute; /* Position at top-left of parent */
  top:1.85em;  /* But, then move down to be below the input */
  border:1px solid #e0e0e0; 
  height:5em;  /* Limit height of list */
  overflow-y:scroll; /* Add vertical scroll bar when list won't fit in height */
}

#list > div {
  cursor:pointer;
  user-select:none;
  margin:2px 0;
}

.highlight {
  background-color:rgba(255, 255, 0, .5);
}
<label for="selectedBrowser">Choose a browser from this list:</label>
<div id="container">
  <input id="selectedBrowser" name="browser">
  <div id="list" class="hidden">
    <div>Chrome</div>
    <div>Firefox</div>
    <div>Internet Explorer</div>
    <div>Opera</div>
    <div>Safari</div>
    <div>Microsoft Edge</div>
  </div>
</div>
Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
  • This is not a cross browser code. And it works only after second click on the input. – williamzo Feb 08 '18 at 21:52
  • 2
    HTML5 is cross-browser with modern browsers. Are you saying you need to support IE 9? – Scott Marcus Feb 08 '18 at 21:53
  • Support IE9 - Yes, I need. And I need select menu after the first click. Not after the second. – williamzo Feb 08 '18 at 21:57
  • And in Chrome I see a very strange behavior. Hover your mouse on this input from your example and you will see a black arrow. In Firefox I don't see this black arrow. And I don't need it in all other browsers including chrome. – williamzo Feb 08 '18 at 22:02
  • Scott Marcus, Thank you very much for your update. But I don't see a classic select menu with the highlight of the options above which the cursor is located. And after typing something in the input the select menu should automatically disappear. – williamzo Feb 08 '18 at 22:20
  • @williamzo I will add this, but these are not requirements you mentioned in your question. – Scott Marcus Feb 08 '18 at 22:21
  • @williamzo I have added that functionality. This is the only way you are going to be able to get what you are looking for. Any additional features you may want are just a matter of adding more event handlers and/or CSS. – Scott Marcus Feb 08 '18 at 22:24
  • Scott Marcus, Yeah, you are absolutely right. I didn't mention this, that's my fault. Hope your updated code will be a good solution of my problem and it will help not only to me. – williamzo Feb 08 '18 at 22:25
  • Scott Marcus, your last update is perfect! And you are so fast, it's amazing! Now your code works perfect and completely solves the task that was described in my question. – williamzo Feb 08 '18 at 22:29
  • Scott Marcus, I don't see how to mark your answer as "the" answer. – williamzo Feb 08 '18 at 22:36
  • Scott Marcus, I'm looking for a developer for browser apps. Can I contact you? – williamzo Feb 08 '18 at 22:38
  • Scott Marcus, I picked your answer as the best answer for my questions. Thanks again. – williamzo Feb 08 '18 at 22:40
  • @williamzo If you look at my profile, on the top-right section, you'll see my website link. You can contact me from there. – Scott Marcus Feb 08 '18 at 22:44
0

HTML5 has a built-in input and a datalist that renders as a combo box. You add a list attribute to the input that matches the value of the id in the datalist. An example is shown below.

<input type="text" list="items" />
<datalist id="items">
  <option>Item1</option>
  <option>Item2</option>
  <option>Item3</option>
  <option>Item4</option>
</datalist>

An issue with this solution is that it is not supported by the Apple Safari browser. W3Schools has the latest compatibility info.

If compatibility is an issue, a number of jQuery or javascript solutions are out there that may solve the problem. Here is a link to a Javascript solution that may work for you.

  • It's not a combox box that renders as an `input` and a `datalist`. It's an `input` and a `datalist` that renders as a combo box. Also, try to find better references than W3 Schools, which is well-known to often have outdated, incomplete or flat out incorrect information. The [Mozilla Developer Network](https://developer.mozilla.org/en-US/docs/Web/HTML) is well-known to be an authoritative source. – Scott Marcus Feb 08 '18 at 22:13
  • Of course, Safari compatibility is a BIG issue. Thank you for your link to a Javascript solution but I don't need any combo boxes. I don't need any arrows at the right of the input. I'll try to look better but from the first view your example don't solve my issue. – williamzo Feb 08 '18 at 22:14