Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display only the text in datalist HTML5 and not the value?

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.

like image 244
Cerebus1504 Avatar asked Sep 02 '14 05:09

Cerebus1504


People also ask

How do you display the text in the Datalist and not its value?

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"> .

Is there a way to apply a CSS style on HTML5 Datalist options?

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.

Which attribute of input element can be used with Datalist element of HTML5?

The list attribute of the input element is used to bind it together with a datalist element.

Is Datalist tag in HTML5?

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.

How to use the datalist tag in HTML?

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.

How to bind input and datalist in HTML?

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.

What is the difference between select Tag and datalist tag?

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.

What do the numbers in the <datalist> tag mean?

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:


1 Answers

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/

like image 154
guest271314 Avatar answered Oct 08 '22 16:10

guest271314