Here is an example:
<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>
http://jsfiddle.net/j7ehtqjd/1/
What I want to achieve is when I click on the input element the value would not be displayed, but ONLY the text (i.e. 1, 2, 3, 4, 5). I.e. the value should be hidden like it is with a general dropdown (<select>
element).
This autocomplete feature is necessary, else I would have gone with the normal dropdown.
To show only the text value of the option in the dropdown, we use the inner text for it and leave out the value attribute. The actual value that we want to send along is stored in a custom data-value attribute: To submit this data-value we have to use an <input type="hidden"> .
Like select elements, the datalist element has very little flexibility in styling. You cannot style any of the suggested terms if that's what your question was asking. Browsers define their own styles for these elements.
The list attribute of the input element is used to bind it together with a datalist element.
The datalist tag is introduced in HTML5. The <datalist> tag should be used with an <input< element that contains a "list" attribute. The value of "list" attribute is linked with the datalist id.
The <datalist> tag is used along with the input element tag. It is used to display the values, which are possible while typing into the input box. The user will have the freedom to enter any value into the input box; just the datalist tag will provide the autosuggestion values.
The <datalist> element's id attribute must be equal to the <input> element's list attribute (this binds them together). The numbers in the table specify the first browser version that fully supports the element. The <datalist> tag also supports the Global Attributes in HTML.
Note the difference between the select tag and the datalist tag. The select tag allows choosing a value from only available options, while the datalist tag just suggests the values from the list. The name attribute is used to only identify the input element in the example.
The numbers in the table specify the first browser version that fully supports the element. The <datalist> tag also supports the Global Attributes in HTML. The <datalist> tag also supports the Event Attributes in HTML. Most browsers will display the <datalist> element with the following default values:
Edit, updated
Following Regent
Try (v3)
html
<input id="selected" list="browsers" name="browser"> <datalist id="browsers"> <option data-value="InternetExplorer" value="1"></option> <option data-value="Firefox" value="2"></option> <option data-value="Chrome" value="3"></option> <option data-value="Opera" value="4"></option> <option data-value="Safari" value="5"></option> </datalist> <input id="submit" type="submit">
js
$(document).ready(function() { var data = {}; $("#browsers option").each(function(i,el) { data[$(el).data("value")] = $(el).val(); }); // `data` : object of `data-value` : `value` console.log(data, $("#browsers option").val()); $('#submit').click(function() { var value = $('#selected').val(); alert($('#browsers [value="' + value + '"]').data('value')); }); });
jsfiddle http://jsfiddle.net/guest271314/j7ehtqjd/13/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With