Activating event listener for the <datalist> element and enhancing the functionality of the <input> element in HTML5

Trying to utilize CSS to make the input box expand on hover/focus of the search ICON in CSS

Attempting to call a function in HTML to an Angular component (seems like CSS cannot be used with datalist element) so that the input tag remains expanded when shifting focus or selecting an item from the datalist

This is example HTML code:

<div class="searchDiv">
    <span class="glyphicon glyphicon-search" id="searchSpan"  (click)="searchEvent($event)"></span>
    <form [formGroup]="searchGroup" (ngSubmit)="searchEvent($event)">
        <input type="text" placeholder="search" id="searchInput" formControlName="localInput" class="" list="promotionsSearchList" (keyup)="searchEvent($event)" />
    </form>
    <datalist id="promotionsSearchList" class="test" onfocus="focusEvent()" onmousemove="focusEvent()">
    </datalist>
</div>

This is the corresponding CSS:

.customClass {
    float: center;
}

.grid {
    display: inline-grid;
    grid-template-columns: 150px 150px 150px 150px auto;
    grid-template-rows: repeat(3,25px);
    width: 100%;
}

#headerDiv {
    grid-row: 1;
    grid-column: 1/6;
    padding-top: 14px;
}

#firstElementSpan {
    grid-row: 1;
    grid-column: 2/6;
}

...

.searchDiv{
    width: 500px;
    vertical-align: middle;
    white-space: nowrap;
    position: relative;
}

...

This is for sample Angular component call:</p>

<pre><code>focusEvent(){
    console.log('onfocusEvent');
    const inputElement = document.getElementById('searchInput');
    inputElement.style.width = '300px';
}

The function does not seem to be called. Tried using Angular's property binding as well but without success. The goal is to keep the input element expanded while focusing on the datalist and navigating through the list. Any assistance would be appreciated.

Answer №1

Many thanks to @CBroe for bringing this to our attention. Unfortunately, we are unable to listen to events or make modifications using datalists. As a solution, I have switched to using unordered lists (ul).

Below is a revised example of the HTML code:

<div class="searchDiv">

      <span class="glyphicon glyphicon-search" id="searchSpan"  (click)="searchEvent($event)"></span>

       <input type="text" placeholder="search" id="searchInput"   class="" list="promotionsSearchList"
       (keyup)="searchEvent($event)" [(ngModel)]="searchModel"/>

       <ul  *ngIf="loadDropDown">
         <li  *ngFor="let pp of promoList" (click)="selectAnyItem($event)" value="pp.id"><span>{{pp.name}}</span>
         </li>
       </ul>

</div>

Here is the CSS styling that I have applied:

 ul {
    padding: 2px;
    list-style-type: none;
     z-index:2;
    background-color:black;
    clear:both;
   }

 li:hover{
       background-color: dodgerblue;
 }

 .rounded-text-box{
      border-radius: 2px;
      border: 2px solid #555;
      padding: 20px;
      width: 200px;
      height: 15px;
  }


  .searchDiv{
       width: 500px;
       vertical-align: middle;
        white-space: nowrap;
       position: relative;
    }

    .searchDiv input#searchInput{

           width: 45px;
           height: 30px;
           background: white;
           border: none;
           font-size: 10pt;
           float: left;
           color: black;
           padding-left: 45px;
           -webkit-border-radius: 5px;
           -moz-border-radius: 5px;
           border-radius: 5px;

           }

         .searchDiv #searchSpan{
             position: absolute;
             top: 5%;
              margin-left: 17px;
             margin-top: 7px;

             z-index : 1;
             color: #4f5b66;
             background-color: inherit;
           }


          .searchDiv input#searchInput:hover, .searchDiv 
         input#searchInput:focus, .searchDiv input#searchInput:active{
          outline:none;
           width:300px;
          } 

          .searchDiv:hover input#searchInput {
              width:300px;
              box-sizing:border-box;
            }

           .searchDiv:hover ul{
               width:300px;
               box-sizing: border-box;
            }

            .searchDiv:hover #searchSpan{
               color: #93a2ad;
            }

I hope this proves helpful for users looking to incorporate similar functionality.

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

The overflow:hidden feature doesn't appear to be functioning as expected

I am struggling to hide the text details within a div containing text and image, organized in ul li structure. Despite applying overflow hidden to the .sbox class, the text details refuse to remain hidden and appear to be overflowing. Explore this issue o ...

Using Bootstrap's CSS classes, is it possible to modify the background color of a table cell?

I'm currently working with Bootstrap 5.0 CSS and attempting to create a table with customized cell colors using some of my own CSS styles. The color property seems to be working correctly, but I'm running into an issue with Bootstrap not recogniz ...

Make multiple calls to gapi.auth2.init using varying client_id each time

I am currently working on a single web page (Angular 6 app) where an admin user can create different Google accounts. In order to obtain a backoffice code with grantOfflineAccess, I am utilizing gapi. However, there seems to be an issue when trying to set ...

How can I keep images from getting cut off when using slick slider with a fixed background?

My current design includes a stylized slick-slider that extends beyond the top border: However, I have set a fixed background for .slick-list, ensuring only the content (text, picture, and button) scrolls: How can I avoid the picture being cropped in t ...

Examining website design on an IOS device from a Windows computer

Is there a way for me to test HTML files from Windows on iOS8 (Local Files)? I have tried Adobe Edge Inspect, but it only works with servers. Are there any free cloud hosts that allow for HTML, CSS, and JS files? Edit: I apologize if my question was not c ...

When you click anywhere on the page, JavaScript's method __dopostback is triggered

I'm encountering an issue in my asp.net application where I have an html table. Specifically, when a user clicks on a td element within the table, I use JavaScript to store the id of that td element in a hidden field. Afterwards, I trigger __dopostbac ...

I am trying to use jQuery to dynamically change classes based on certain conditions. However, I am encountering an issue where the class of my 'home' link remains unchanged even after clicking on another link. How

How can I modify my jQuery code to achieve the following: If the 'home page' is active, then apply CSS class A (dark green color) to the 'home link'; otherwise, apply CSS class B (brown color). (In essence, I want the relevant link to ...

The default filterMatchType in PrimeNG DataTable Filtering is not set

I can't seem to make a data column filter work with a "contains" match type. Does anyone have any suggestions? Check out this Plunker link, and try filtering by "f70": <p-dataTable [value]="orderList"> <p-column field="itemDesc" header= ...

"Can you provide tips on how to automatically close a dropdown select menu when the mouse

<div class="row"> <div class="col-sm-6 no-right-border"> <div class="form-group"> <label for="inputFirstName">Filter:</label> <select class="form-control c ...

Text hidden within the image, each line concealing a separate message

I am having an issue where my image is overlaying the text. I would like the text to wrap around the image and for any hidden text to appear on a different line. How can this be achieved? Here is the CSS code I am using: div.relative { position: relati ...

Issue with AdminLite 2.4.0 data table functionality malfunctioning

Check out this template that I'm using. I've copied all the contents for the bower_components and dist folders, and made sure to link and require everything properly. There are no 404 errors, only status code 200. Here is a snippet of my code: ...

Using CSS to cut out a triangle shape from an image

Our designer has a vision for this: However, I'm struggling to find a suitable method for creating the triangle crop effect: Implementing an :after element that spans the entire width and utilizes border tricks to form a triangle shape Creating a l ...

The bottom margin of the last element does not affect the size of its container

I'm curious as to why the margin-bottom of .content-b isn't affecting the height of the .container. .container { background: grey; } .content-a, .content-b { height: 100px; border: 1px solid red; margin-bottom: 100px; } <div class= ...

The angular schematic workflow encountered an error while attempting to create a new project

Successfully installed Angular CLI, but I encountered an issue while creating a new project using 'ng new project name'. Some packages were created, but the installation failed with an unexpected end in JSON while parsing near.....'A85qs.... ...

Please only choose the immediate children of the body element

<body> <div> <div class='container'></div> </div> <div class='container'></div> </body> Is there a way to use jQuery to specifically target the second container div dire ...

Calculating the total sum of an array utilizing filter and reduce techniques

How can I calculate the total value of all my products in the list by adding numbers together? this.totalValue = this.items.filter((item) => item.qtyvalue) .map((item) => item.qtyvalue) .reduce ...

What is the best way to position a <td> element within a table row underneath another <td>?

Looking for a solution to ensure Content 3 appears below Content 2 in this block of HTML. Content 1 is extensive and extends far down the page. <table> <tr> <td>(Content 1)</td> <td>(Content 2)</td> ...

Unit testing the TypeScript function with Karma, which takes NgForm as a parameter

As I work on writing unit tests for a specific method, I encounter the following code: public addCred:boolean=true; public credName:any; public addMachineCredential(credentialForm: NgForm) { this.addCred = true; this.credName = credentialForm.val ...

Activate browser scrollbar functionality

Below is the HTML code snippet: <html> <head> <style> #parent { position : absolute; width : 500px; height : 500px; } #top { position : absolute; width : 100%; height: 100%; z-index : 5; } #bottom { position : absolute; width : 100%; ...

Inquiring about the usage of div elements in constructing an HTML webpage

When designing my own web pages, I consistently encounter issues with using divs for a multi-column layout. Despite utilizing the float attribute to align columns left or right, resizing the browser window often causes them to stack on top of each other an ...