How can I efficiently make a dropdown menu with search functionality instead of using datalist?

I am currently working on creating a custom searchable input field that makes backend calls. Instead of using datalist, I want to design a unique UI with ul and li items. While I have successfully implemented the search functionality, I am facing a glitch with the UI. I am trying to display the ul when the input is focused, but when I attempt to hide the ul on blur of the input, the ul disappears before I can select an item. What would be the correct CSS approach for this scenario?

<input placeholder (blur)=“focussed=false” (focus)=“focussed=true” (keyup)=“filterData($event.target.value) ”/>
<ul *ngIf=“focussed”>
<li *ngFor=“item of filteredItems” >{{item}}</li>
</ul> 

Answer №1

To improve item selection, consider using the (mousedown) event instead of (click). The mousedown event takes precedence over blur, ensuring it is fired first.

If you have a selection handler for list items, replace your li tag with the following code:

<li *ngFor="let item of filteredItems" (mousedown)="handleItemClick(item)">
     {{item}}
</li>

Within the handleItemClick method, you can set focussed to false to close the list only after an item is selected.

handleItemClick(item){
   // your code
   this.focussed = false; // close the list
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Words not within the span, away from the span border

I’m currently in the process of creating a dedicated section on my website called "Voices of the Nation,” which draws inspiration from the foundations laid out in the U.S. Constitution. The objective is to highlight individuals who have shown their sup ...

The jQuery method .on gathers and retains click events

I created a component that manages a view containing articles with games. In order to prevent memory overload and optimize performance, I implemented a solution where when a user clicks on an article (each having the class "flashgame"), they can choose to ...

Troubleshooting Flexbox in Internet Explorer 11: Issue with 100% width not functioning properly within nested flex containers with a 'column'

Is there a way to ensure that an image within nested flex containers has a width of 100% in IE11, even when the container has flex-direction: column? I've tried setting the width to 100% with a max-width of calc(100% - 0.1px), but unfortunately it doe ...

Popper.js failing to initialize during page load

After attempting to initialize Popper.js as shown below, I am encountering an issue where nothing is happening. $().ready(function(){ $('[data-toggle="tooltip"]').tooltip(); }); Interestingly, when I apply the same code to a button, it w ...

Angular - Ensuring the Independence of Field Pairs During Validation

I need to independently validate two sets of fields. One set is for passwords, which should match each other. The second set is for email addresses, which should also match. The current method I have tried isn't working. Can someone guide me on how ...

How to Extract Specific Data from JSON Response using Jquery?

How do I extract specific fields from this Json Result or response with the following structure? var WPQ3ListData = { "Row" : [{ "ID": "27", "PermMask": "0x400001f07fff1bff", "FSObjType": "0", "Title": "NOOO", "FileLeafRef": "27_.000", "Total ...

Can I incorporate NodeJS modules such as "oauth" into my Firefox extension?

I am diving into the world of Firefox addons for the first time. My project entails making calls to Twitter's APIs, which means I require an oauth library to kick things off. After some research, I came across an existing library in the npm package r ...

When the browser's back button is clicked, no action occurs with Next/router

I am confused about why my page does not reload when I use the browser's back button. On my website, I have a catalog component located inside /pages/index.js as the home page. There is also a dynamic route that allows users to navigate to specific p ...

Guide on exporting member formModule in angular

After compiling my code in cmd, an error message is displayed: ERROR in src/app/app.module.ts(3,10): error TS2305: Module '"C:/Users/Amir_JKO/my-first-app/node_modules/@angular/forms/forms"' does not have an exported member 'formModul ...

Checking for at least one matching pair in a JavaScript object

I have a pair of dictionaries that I need to compare in JavaScript. The goal is to determine if there is at least one identical key-value pair between them, not necessarily the entire dictionary being identical. my_dict = {"Text1":"Text1", "Text2":"Text3", ...

Issue with ScrollBar functionality in IE

Hello, I am experiencing an issue with the vertical scrollbar on the addUser Page. When I try to select multiple values in the dropdown menu, the page length increases but the scrollbar does not function correctly. This problem only seems to occur in Inter ...

Using Typescript with Protractor for Dropdown Menus

As a newcomer to Protractor, I am looking to automate the selection of a dropdown. While I have some knowledge in JavaScript, I am currently working with typescript. Can someone advise me on how to select the dropdown option based on the text provided? Fo ...

Utilizing NgClass Within an Attribute Directive in Angular 2.4.0

Is there a way to utilize NgClass within a custom attribute directive to modify the CSS class of the main elements? For example, if I have this code snippet: @Component({ selector: 'my-app', template: ` <div> <div class=" ...

Enhancing User Experience by Applying Hint Text to Multiple Input Fields using jQuery

I am facing a situation where I need to add hint text to an input field using jQuery, as HTML5 placeholder is not an option in this case. I have implemented the functionality successfully for one input field, as shown below: $("#reportMailerCustomFrom"). ...

Tips for checking dropzone functionality during development

I'm currently working on an Angular 5 application and incorporating dropzonejs (the angular wrapper) into it. However, as I am not the backend developer, I do not have full visibility into how the backend has been developed. Currently, CORS is being ...

Can we display an express view in response to a WebSocket event?

My goal is to display an express view within a websocket event as shown below: socket.on('name', function () { //prompt the client to render a specific express view }); ...

Tips for preventing control click events from interfering with modal dialog interactions

From what I understand, when I open a modal dialog in jQuery, all input controls will be disabled. However, I am still able to click on buttons, divs, and other controls. Is there a way in jQuery to disable all interactions once the modal dialog is opene ...

Can Node.js version 11 support node-sass in Angular projects?

I'm encountering a compilation error with my code. Could this be due to the node version? Does node-sass work with all versions of node? If not, how can I resolve this issue? Compilation error: Unable to locate module 'node-sass' ...

How to retrieve an element using a dynamically generated class name in Vue.js

<v-data-table :headers="menuheaders" //this menus from api response :items="menus" item-key="usersmenu_menuid" items-per-page="1000" hide-default-footer="" class="elevation-1" > <template v-s ...

Steps to implement a text border in Raphael.js

I have a website dedicated to creating customized football jersey designs. I'm utilizing Raphael.js for customization, and I am looking for a way to add an outline to text using Raphael. My research led me to the suggestion of using the stroke proper ...