I got a input box, when the focus event fired on it, a list of prompt text will show up below the input box, and when blur event is fired, list will be hidden.
I add click event listener on the list of prompt, but when I try to click the prompt, blur event on the input box fired first so the click event on the prompt never get fired.
Is there a way to make click event on the prompt text fire first? After that I will fire the blur event handler to hide the prompt text.
var textEl = document.getElementById("text"),
listEl = document.getElementById("list");
textEl.onfocus = function() {
listEl.style.display = "initial";
}
textEl.onblur = function() {
listEl.style.display = "none";
}
listEl.onclick = function(event) {
var item = event.target;
alert(item.textContent);
textEl.value = item.textContent;
}
#container {
position: relative;
width: 300px;
margin: auto;
}
input {
width: 100%;
}
#list {
display: none;
position: absolute;
width: 100%;
background: #fff;
}
.item {
padding: 12px 0;
border-bottom: 1px solid #ccacac;
}
<div id="container">
<input type="text" id="text">
<div id="list">
<div class="item"> item 1</div>
<div class="item"> item 2</div>
<div class="item"> item 3</div>
</div>
</div>