Show Autocomplete Only After 3 Entered Chars In
Is it possible to autocomplete only after a user entered at least 3 letters? Here is my current code: HTML/PHP:
Solution 2:
You need to use javascript and listen to the keyup
event for your input element.
Then you check if the value of the input is longer than 3 character and if it is, you need to make an ajax request to your server to get the related names and use them to fill the datalist
Solution 3:
Just thought I'd share what I did using setAttribute
and removeAttribute
on the keyUp
and keyDown
to display the datalist (dir
) when the text box contains at least 3 characters and handling the Backspace (keyCode=8
) to prevent the full list from showing as it widens the search results.
<inputtype="search"name="q"id="q"autofocus="true"onkeyup="if(this.value.length>=3)this.setAttribute('list','dir');"onkeydown="if(this.value.length<3||event.keyCode==8)this.removeAttribute('list');"placeholder="Search Staff Directory"
/>
Hope this helps anyone looking for a simple solution that does not require jQuery or Ajax, just a simple datalist.
Post a Comment for "Show Autocomplete Only After 3 Entered Chars In