{"id":2072,"date":"2024-05-30T14:41:14","date_gmt":"2024-05-30T21:41:14","guid":{"rendered":"https:\/\/www.angulartraining.com\/daily-newsletter\/?p=2072"},"modified":"2024-05-30T14:41:14","modified_gmt":"2024-05-30T21:41:14","slug":"auto-filtering-a-dropdown-list-with-datalist","status":"publish","type":"post","link":"https:\/\/www.angulartraining.com\/daily-newsletter\/auto-filtering-a-dropdown-list-with-datalist\/","title":{"rendered":"Auto-filtering a dropdown list with datalist"},"content":{"rendered":"\n<p>At times, we developers are so focused on Angular and TypeScript that we forget that even the basics, such as HTML and CSS, are always evolving. In that spirit, I&#8217;ve covered some <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/hidden-gems-of-html\/\" target=\"_blank\" rel=\"noopener\" title=\"\">hidden gems of HTML in the past<\/a>.<\/p>\n\n\n\n<p>For instance, I&#8217;ve seen <a href=\"https:\/\/www.angulartraining.com\/Angular%20Training%20Case%20Studies.pdf\" target=\"_blank\" rel=\"noopener\" title=\"\">numerous consulting clients<\/a> use a specific library for fancy auto-filter or auto-complete dropdowns. While the approach works, <a href=\"https:\/\/blog.angulartraining.com\/how-to-pick-the-right-dependencies-for-your-angular-application-328ce637b982\" target=\"_blank\" rel=\"noopener\" title=\"\">adding too many dependencies is the number 1 reason people get stuck with an old version of Angular<\/a>.<\/p>\n\n\n\n<p>Enter <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/HTML\/Element\/datalist\" target=\"_blank\" rel=\"noopener\" title=\"\">datalist<\/a>, a simple HTML element that auto-suggests and filters based on user input:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"821\" height=\"332\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/05\/datalist.gif\" alt=\"\" class=\"wp-image-2073\"\/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>How&#8217;s that for a low-code, no-dependency solution? <code>datalist<\/code> works very much like a <code>select<\/code> element, using a list of <code>options<\/code> to be rendered. What connects the <code>datalist<\/code> to an <code>input<\/code> is the <code>list<\/code> attribute on the input that refers to the <code>id<\/code> of the <code>datalist<\/code> element:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"316\" height=\"23\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/05\/image-12.png\" alt=\"\" class=\"wp-image-2074\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/05\/image-12.png 316w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/05\/image-12-300x22.png 300w\" sizes=\"auto, (max-width: 316px) 100vw, 316px\" \/><\/figure>\n<\/div>\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"334\" height=\"24\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/05\/image-13.png\" alt=\"\" class=\"wp-image-2075\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/05\/image-13.png 334w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/05\/image-13-300x22.png 300w\" sizes=\"auto, (max-width: 334px) 100vw, 334px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>And that&#8217;s it. On a side note, <code>datalist<\/code> is also compatible with many other input types, such as <code>time<\/code> or <code><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/HTML\/Element\/datalist#color_type\" target=\"_blank\" rel=\"noopener\" title=\"\">color<\/a><\/code>:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"388\" height=\"187\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/05\/image-14.png\" alt=\"\" class=\"wp-image-2077\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/05\/image-14.png 388w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/05\/image-14-300x145.png 300w\" sizes=\"auto, (max-width: 388px) 100vw, 388px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>This results in:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"147\" height=\"154\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/05\/image-15.png\" alt=\"\" class=\"wp-image-2078\"\/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>At times, we developers are so focused on Angular and TypeScript that we forget that even the basics, such as HTML and CSS, are always evolving. In that spirit, I&#8217;ve covered some hidden gems of HTML in the past. For instance, I&#8217;ve seen numerous consulting clients use a specific library for fancy auto-filter or auto-complete [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[],"class_list":["post-2072","post","type-post","status-publish","format-standard","hentry","category-html"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/2072","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/comments?post=2072"}],"version-history":[{"count":3,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/2072\/revisions"}],"predecessor-version":[{"id":2080,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/2072\/revisions\/2080"}],"wp:attachment":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/media?parent=2072"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/categories?post=2072"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/tags?post=2072"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}