<!DOCTYPE html>
<html>
<body>
<input list="browsers" name="browser">
<datalist id="browsers">
<option value="Internet Explorer">1</option>
<option value="Firefox">2</option>
<option value="Chrome">3</option>
<option value="Opera">4</option>
<option value="Safari">5</option>
</datalist>
<input type="submit">
</form>
</body>
</html>
http://jsfiddle.net/j7ehtqjd/1/
I have attached a fiddle. Please check. The solution i want to achieve is when i click the dropdown the value is displayed, but i want the text 1,2,3,4,5 to be displayed. I have to use a json response for my actual problem.Only the text should be displayed and the value should be hidden like a general dropdown. This autocomplete feature is necessary else i would have gone with the normal dropdown.
A quite messy method for doing this without dependencies like jQuery (edit: I realise that jQuery is being used by the OP, but this method will integrate fine with or without jQuery, so might helps others who are not).
First, swap your value and text nodes around, like so:
This ensures that the input always shows the correct value for the user. Then add in some JavaScript to check the text node of the corresponding value, like so:
You can also change the
'change'
event listener to'keyup'
too, to get real-time feedback. And instead ofconsole.log('The value is:', _value )
you can simply add in an additionalhidden
input to store the value, thus allowing you to send both the value and id.This uses modern JavaScript notation, but to make it backward compatible just need to change the
'change', () => {
call to'change', function() {
and changelet
tovar
.Edit, updated
Following Regent
Try (v3)
html
js
jsfiddle http://jsfiddle.net/guest271314/j7ehtqjd/13/