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

What is the best way to adjust the width of a column grid header in extjs

When adjusting the width of a vertical header in a grid to 50px, the header text disappears unless manually dragged to the left. How can I ensure the header text remains visible even with a smaller header size? Consider the following code snippet: { text ...

Implementing a scroll-triggered class addition in Angular

I am currently refreshing my knowledge of Angular. After installing AngularCLI, I am attempting to transition from using jQuery to add a class on scroll. Would it be necessary to utilize [ngClass] in order to check a variable with window location? Right ...

Ensure that all form data is cleared upon the user clicking the back button

Is there a way to automatically clear the contact form data when a user navigates back in their browser? This would help prevent spammers from submitting the form multiple times by constantly clicking back and resubmitting. ...

Adjust the color of the font within a div element when hovering over it

I've been attempting to modify the text color and add an underline when a user hovers over it. Despite trying various methods, I haven't been successful. I scoured the internet for a solution but couldn't find one that met my specific requi ...

Background image not displaying

My web page has a background image set using this CSS: body, html { background-image: url(Content/Images/bg-lounge-2-l.jpg); background-repeat: repeat; background-attachment: fixed; /*background-position: 0 -390px;*/ } ...

Angular: merging multiple Subscriptions into one

My goal is to fulfill multiple requests and consolidate the outcomes. I maintain a list of outfits which may include IDs of clothing items. Upon loading the page, I aim to retrieve the clothes from a server using these IDs, resulting in an observable for e ...

Is there a way to enlarge an iFrame to fill the entire screen with just a button click, without using JavaScript

I am currently attempting to achieve full screen mode for an iFrame upon clicking a button, similar to the functionality on this site. On that website, there is a bar that expands to full screen along with the embedded game, which is what I am aiming for. ...

Angular4 Leaflet Map encountering errors

Here is the template: <div id="mapid" style="height: 500px"></div> After installing Leaflet and the typings for Leaflet, I encountered an error stating that the map container was not found. To solve this, I added its import. This is the cont ...

Issue with manipulating background color with JQuery in CSS

Currently, I have a simple navigation menu consisting of five links. The navigation bar is styled with a background image and changes to a darker shade when hovered over. My goal is to implement a jQuery script that dynamically changes the color of the act ...

Should the getDownloadURL() from Firebase Storage be regenerated every time, or can it be reused?

Should I store and reuse the URL received from angularfire2's getDownloadURL() in the database instead of calling getDownloadURL() every time I want to show an image from Firebase Storage? Whenever I use getDownloadURL() in my component, the images a ...

Utilizing Selenium for the Upload of an APK Document

I've encountered an issue during my test. I'm attempting to upload an APK file, but the process seems to halt at that stage. My attempts with simple sendKeys using the file path and an AutoIT script have both proven unsuccessful. Below is my in ...

What's the Hold-Up with IntersectionObserver in Brackets?

As a novice in the world of web development, I have been utilizing Brackets as my main tool. Recently, I've encountered a few hurdles specifically related to javascript. One issue I'm facing is trying to implement lazy loading on a div containing ...

406 error is triggered by GET parameters

Here is a PHP script I have: <!DOCTYPE html> <html lang="es-ES" xmlns="http://www.w3.org/1999/xhtml" xml:lang="es-ES"> <head> <meta charset="iso-8859-1" /> </head> <body><p><?php if(isset($_GET['echo& ...

Angular: Utilizing Parameters in HTTP GET Requests

I've recently started using Angular. Currently, I'm working on sending a parameter in an HTTP GET request. This is what my code looks like: for (var i = 0, len = this.recentArtists.topartists.artist.length; i < len && i < h ...

The configuration for CKEditor5's placeholder feature seems to be malfunctioning

I am currently experimenting with a customized version of CKEditor 5 known as BalloonBlockEditor. Below is the custom build output that I have created: /** * @license Copyright (c) 2014-2023, CKSource Holding sp. z o.o. All rights reserved. * For licens ...

Error: TypeScript Knockout table failing to display data

I have a table that displays invoices, along with a nested table showing the individual checks associated with each invoice. I am using knockout and typescript to render these tables. Currently, I can successfully display the invoices but am facing difficu ...

Does this fall under the category of accepted practices for employing an effect in Angular?

I am working on integrating an Angular directive with a signal that receives values from a store selector. This signal is crucial for determining whether a button should be disabled or not. I'm curious about the best approach to listen to this signal ...

Angular time-based polling with conditions

My current situation involves polling a rest API every 1 second to get a result: interval(1000) .pipe( startWith(0), switchMap(() => this.itemService.getItems(shopId)) ) .subscribe(response => { console.log(r ...

Creating packages for multiple Typescript projects that rely on a shared local module

I am currently developing a series of VSTS extensions, with each extension being its own independent Node project complete with its own package.json file and node_modules folder. The structure of the folders is as follows: - MyExtension - package.json ...

Updating Sencha list itemTpl on the fly

Is there a way to dynamically change the itemTpl in a Sencha list to adjust the visibility of a column based on certain conditions? Your assistance would be greatly appreciated. ...