The following is a simplified version of the input drop-down I am working with.
The main summary of what he does is: if you focus on typing, a drop-down list will appear. If you select one of the options from the drop-down list, the option will fill in the input and the drop-down menu will disappear. This is achieved using onfocus
and the functions that I called dropdown();
and undropdown();
.
I have a dilemma when I cannot make the drop-down list disappear when someone clicks elsewhere. If I use onblur
, it successfully hides the drop-down list, but if you click on the parameter, it will not fill the input, this is because the onblur
function onblur
launched first, and therefore the input();
function input();
doesn’t start because the dropdown menu is already hidden.
If you put the onclick
tag in the body tag or another parent element, it treats onfocus
as a click, where it runs the dropdown();
function dropdown();
, and then the undropdown();
function undropdown();
, so the drop-down menu never appears overlapping functions.
I would appreciate help in figuring out how to arrange the functions so that they execute in the correct order without overlapping each other.
JSFiddle is available here.
function input(pos) { var dropdown = document.getElementsByClassName('drop'); var li = dropdown[0].getElementsByTagName("li"); document.getElementsByTagName('input')[0].value = li[pos].innerHTML; undropdown(0); } function dropdown(pos) { document.getElementsByClassName('content')[pos].style.display = "block" } function undropdown(pos) { document.getElementsByClassName('content')[pos].style.display = "none"; }
.drop { position: relative; display: inline-block; vertical-align: top; overflow: visible; } .content { display: none; list-style-type: none; border: 1px solid #000; padding: 0; margin: 0; position: absolute; z-index: 2; width: 100%; max-height: 190px; overflow-y: scroll; } .content li { padding: 12px 16px; display: block; margin: 0; }
<div class="drop"> <input type="text" name="class" placeholder="Class" onfocus="dropdown(0)"/> <ul class="content"> <li onclick="input(0)">Option 1</li> <li onclick="input(1)">Option 2</li> <li onclick="input(2)">Option 3</li> <li onclick="input(3)">Option 4</li> </ul> </div>
PS: In addition to the above problem, I would appreciate a change to get a better name for this question, so that someone experiencing a similar problem could find it more easily.
source share