{"id":2357,"date":"2018-08-13T21:32:28","date_gmt":"2018-08-14T02:32:28","guid":{"rendered":"http:\/\/bluegalaxy.info\/codewalk\/?p=2357"},"modified":"2018-08-13T21:34:11","modified_gmt":"2018-08-14T02:34:11","slug":"html-how-to-make-a-combo-box-using-datalist","status":"publish","type":"post","link":"https:\/\/bluegalaxy.info\/codewalk\/2018\/08\/13\/html-how-to-make-a-combo-box-using-datalist\/","title":{"rendered":"HTML: How to make a combo box using datalist"},"content":{"rendered":"<p>A combo box is a combination text box and select menu, rolled into one. It uses a little known HTML5 element called &#8220;datalist&#8221;, which is supported by all of the modern browsers, but was never supported by Safari or late versions of Internet Explorer.<\/p>\n<p>To set up an HTML5 combo box, start by creating a text input, and give this text input a list attribute that matches the id of the datalist. For example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"html\">&lt;input type=\"text\" value=\"\" list=\"colorlist\" class=\"b\"\/&gt;<\/pre>\n<p>Notice list=&#8221;colorlist&#8221;.<\/p>\n<p>Then set up the datalist, in much the same way as you would set up a select menu. For example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"html\">  &lt;datalist id=\"colorlist\"&gt;\r\n      &lt;option value=\"Black\"&gt;&lt;\/option&gt;\r\n      &lt;option value=\"Blue\"&gt;&lt;\/option&gt;\r\n      &lt;option value=\"Dark Green\"&gt;&lt;\/option&gt;\r\n      &lt;option value=\"Grey\"&gt;&lt;\/option&gt;\r\n      &lt;option value=\"Green\"&gt;&lt;\/option&gt;\r\n      &lt;option value=\"Indigo\"&gt;&lt;\/option&gt;\r\n      &lt;option value=\"Red\"&gt;&lt;\/option&gt;\r\n      &lt;option value=\"Violet\"&gt;&lt;\/option&gt;\r\n      &lt;option value=\"White\"&gt;&lt;\/option&gt;\r\n      &lt;option value=\"Yellow\"&gt;&lt;\/option&gt;\r\n  &lt;\/datalist&gt;<\/pre>\n<p>Now if you place your cursor in the text field and hit the down arrow, you will see a select menu.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2358\" src=\"http:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2018\/08\/hrml-combo-box.png\" alt=\"\" width=\"258\" height=\"247\" \/><\/p>\n<p>Also if you type a letter, a select menu will appear below, filtered to the letter typed.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2359\" src=\"http:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2018\/08\/combo-box-g.png\" alt=\"\" width=\"257\" height=\"212\" \/><\/p>\n<p>Note: In Firebox, the menu will be filtered to any entries that include the letter, while Chrome will be filtered to any entries that start with the letter.<\/p>\n<p>Here is the complete HTML:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"html\">  &lt;input type=\"text\" value=\"\" list=\"colorlist\" class=\"b\"\/&gt;\r\n  &lt;datalist id=\"colorlist\"&gt;\r\n      &lt;option value=\"Black\"&gt;&lt;\/option&gt;\r\n      &lt;option value=\"Blue\"&gt;&lt;\/option&gt;\r\n      &lt;option value=\"Dark Green\"&gt;&lt;\/option&gt;\r\n      &lt;option value=\"Grey\"&gt;&lt;\/option&gt;\r\n      &lt;option value=\"Green\"&gt;&lt;\/option&gt;\r\n      &lt;option value=\"Indigo\"&gt;&lt;\/option&gt;\r\n      &lt;option value=\"Red\"&gt;&lt;\/option&gt;\r\n      &lt;option value=\"Violet\"&gt;&lt;\/option&gt;\r\n      &lt;option value=\"White\"&gt;&lt;\/option&gt;\r\n      &lt;option value=\"Yellow\"&gt;&lt;\/option&gt;\r\n  &lt;\/datalist&gt;<\/pre>\n<p>&nbsp;<\/p>\n<p>If you need to find a solution that will work with older browsers and Safari, here is some JavaScript that can be used:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">var nativedatalist = !!('list' in document.createElement('input')) &amp;&amp; \r\n    !!(document.createElement('datalist') &amp;&amp; window.HTMLDataListElement);\r\n\r\nif (!nativedatalist) {\r\n  $('input[list]').each(function () {\r\n    var availableTags = $('#' + $(this).attr(\"list\")).find('option').map(function () {\r\n      return this.value;\r\n    }).get();\r\n    $(this).autocomplete({ source: availableTags });\r\n  });\r\n}<\/pre>\n<p>Here is a complete working example on codepen:<\/p>\n<p class='codepen'  data-height='265' data-theme-id='0' data-slug-hash='VBRoVm' data-default-tab='html,result' data-animations='run' data-editable='' data-embed-version='2'>\nSee the Pen Combo Box (JavaScript) by Chris Nielsen (@Chris_Nielsen) on CodePen.<\/p>\n\n<p>&nbsp;<\/p>\n<p>For more information about HTML5 datalist, see:<br \/>\n<a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/HTML\/Element\/datalist\">https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/HTML\/Element\/datalist<\/a><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A combo box is a combination text box and select menu, rolled into one. It uses a little known HTML5 element called &#8220;datalist&#8221;, which is supported by all of the modern browsers, but was never supported by Safari or late versions of Internet Explorer. To set up an HTML5 combo box, start by creating a &hellip; <a href=\"https:\/\/bluegalaxy.info\/codewalk\/2018\/08\/13\/html-how-to-make-a-combo-box-using-datalist\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">HTML: How to make a combo box using datalist<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[104],"tags":[10,173,81,45],"class_list":["post-2357","post","type-post","status-publish","format-standard","hentry","category-html-css","tag-css","tag-datalist","tag-html5","tag-javascript"],"_links":{"self":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/2357","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/comments?post=2357"}],"version-history":[{"count":5,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/2357\/revisions"}],"predecessor-version":[{"id":2365,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/2357\/revisions\/2365"}],"wp:attachment":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/media?parent=2357"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/categories?post=2357"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/tags?post=2357"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}