Make sure to pause and wait for a click before diverting your

Having an issue with a search dropdown that displays suggestions when the search input is focused. The problem arises when the dropdown closes as soon as the input loses focus, which is the desired functionality. However, clicking on any suggestion causes the box to close before registering the click for that specific suggestion.

Here's my current code:

<div id="demo-2">
 <input
   type="search"
   placeholder="Search By Title, Author"
 />
 <div class="autocomplete">
   <mdb-card *ngFor="let book of books" (click)="logger(book.id)">
     <!--some code-->
   </mdb-card>
 </div>
</div>

And here's the CSS snippet:

#demo-2 input[type="search"]:focus {
  width: 275px;
  color: #000;
  background-color: #fff;
  cursor: auto;

  ~ .autocomplete {
    visibility: visible;
  }
}

.autocomplete {
  height: 350px;
  width: 275px;
  position: absolute;
  visibility: hidden;
  overflow: auto;
}

Lastly, a part of the TypeScript code:

  logger(id) {
    console.log(id); 
  }

I've been struggling with this for over a day now. Any assistance would be greatly appreciated.

Answer №1

Define a new variable called showContent in your TypeScript file:

showContent = false;

displayMessage(id) {
  console.log(id);
  showContent = false;
}

hideContent() {
  showContent = false;
}

inputSelected() {
  showContent = true;
}

Update your HTML code accordingly:

<div id="example-22">
 <input
   type="text"
   placeholder="Enter text here" (focusout)="hideContent()"
 />
 <div class="popup" [ngStyle]="{'visibility':showContent ? 'visible' : 'hidden'}">
   <custom-element *ngFor="let item of items" (click)="displayMessage(item.id)">
     <!--content goes here-->
   </custom-element>
 </div>
</div>

Do not forget to eliminate the visibility property from your CSS stylesheet.

Answer №2

Instead of using ~.autocomplete on the div element, consider applying it to mdb-card in order to affect its sibling elements with the specified properties.

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

"An error occurred: Uncaught SyntaxError - The import statement can only be used within a module. Including a TypeScript file into a

I need to integrate an Angular 10 TypeScript service into a jQuery file, but I am facing an issue. When I try to import the TypeScript service file into my jQuery file, I encounter the following error: Uncaught SyntaxError: Cannot use import statement outs ...

Sending a CSS class to an Angular library

In my development process, I am currently working on creating a library using Angular CDK specifically for custom modals. One feature I want to implement is the ability for applications using the library to pass a CSS class name along with other modal conf ...

Accessing a precise div element in a webpage

Currently, I am utilizing Vue.js and Nuxt for my web development. One issue I am facing is related to anchors. Specifically, when I navigate to mysite.com/page1#section1, I want the page to scroll to section1. In my code, I have the following snippet: < ...

Exploring the implementation of enums in a separate file with Angular Ahead-Of-Time compilation

Converting a well-functioning JIT-based scenario angular-based app (angular-cli) to an AOT-based scenario. The AOT-build is successfully running. Upon opening the resulting webpage, I am encountering errors stating 'Cannot read property 'none&ap ...

What strategies can be employed to mitigate the activation of the losing arm in a Promise.race?

My current task involves sending the same query to multiple identical endpoints (about five) across various Kubernetes clusters. The goal is to aggregate the results without any delays and report failures to the user while continuing with the process seaml ...

Ways to resolve the issue of the missing property 'ganttContainer' on the 'Gantt' type

I encountered an issue while trying to utilize the Gantt chart feature from the Dhtmlx library in TypeScript. The problem seems to stem from an error during the initialization of gantt. How can I go about resolving this? Below is the relevant code snippet: ...

Calculation for determining the fill of a DIV's remaining height

Here is what I have: <div id="modal-container"> <div id="modal-body"></div> <div id="modal-footer"></div> </div> I am currently working on a JavaScript function to adjust the height of #modal-body to fill in the re ...

How can I pass custom QueryParams to Router's NavigationExtras in Angular and create a personalized navigation experience?

I am currently working on implementing data filtering functionality in my Angular application. The concept is fairly simple - the user selects filters from dropdown menus and then clicks a button to add key-value pairs of filters to the URL route. There a ...

Steps for implementing an onclick action in Angular 4 from infowindow content

Currently, I am integrating Google Maps into an Angular 4 project and I need to implement a click event inside the infowindow. How can I achieve this? I attempted the following code but encountered an issue where the name is undefined. Even though I called ...

Is it possible to animate the innerHTML of a div using CSS?

In my HTML file, I have these "cell" divs: <div data-spaces class="cell"></div> When clicked, the innerHTML of these divs changes dynamically from "" to "X". const gridSpaces = document.querySelectorAll("[data-spaces]"); f ...

In Angular, is there a way to transform time into the format of YYYY-MM-DDThh:mm:ssTZD?

Our backend is built with Java and we are using the ISO 8601 standard for date formatting. In order to pass dates in this format, I require a method to convert the date into the specified format. In Java, we use: DateFormat iso8601 = new SimpleDateFormat( ...

How can I add a new key value pair to an existing object in Angular?

I'm looking to add a new key value pair to an existing object without declaring it inside the initial object declaration. I attempted the code below, but encountered the following error: Property 'specialty' does not exist on type saveFor ...

Problem with special characters in Localstorage for Internet Explorer 8

After developing a jQuery script that retrieves data from a service, stores it in local storage, and displays it on the page upon a specific event, I encountered an issue with displaying Chinese characters only on IE8+ browser. Unfortunately, IE is unabl ...

Issue: The system needs the 'image' property to proceed (Dall E's image variation API by OpenAI)

Utilizing the Dall E Create image variation API has been a bit challenging for me. Every time I send a post request, I encounter this particular error: error: { "code": null, "message": "'image' is a required property&q ...

The Intl.DateTime formatter consistently generates incorrect times even after refreshing the component in a React application

Our component is responsible for receiving information from the backend and rendering it. The properties of the component are defined as: interface CitizenshipProps { citizenship?: Citizenship; name?: string; lastName?: string; onUpdateClic ...

Clearing HTML Text Input Box When User Presses Enter

Currently, I am working on a lengthy program that is functioning almost flawlessly. However, there is one persistent issue: whenever I hit the Enter key while the text box is clicked, it clears the box and appends the data to the URL as if it were a GET re ...

How can I implement a feature in Angular where clicking the edit button loads a form (in a separate component) pre-populated with previous data, along with an update button for

Within the employee-list component, there is a table displaying a list of employees. This table includes an option to edit details. <button type="button" class="btn btn-primary" routerLink="../create-employee">Edit</b ...

"Troubleshooting the issue of Angular's select binding causing a disruption

The Angular version being used is 1.4.7. Within the model in question, there are two objects: 'systems', which is an array, and 'selectedSystem'. The desired outcome is for 'selectedSystem' to reference one of the objects wit ...

I don't understand why my BeautifulSoup code is throwing an attribute error even though the variable it's referring to is assigned a value

Currently, I am working with Python 3.9.1 along with selenium and BeautifulSoup to develop my first web scraper targeting Tesco's website. This is essentially a mini project that serves the purpose of helping me learn. However, upon running the code p ...

"Exploring the world of Ionic 2: uncovering its public variables

I'm facing an issue with Ionic 2, specifically with Angular. My concern revolves around a variable called "isConnected". I am unable to access it from an internal function within a function as it gives me an error saying "can't define property of ...