What is the method to have text added automatically to an input field when a key is pressed?

Within my navigation, I have implemented a "full-screen overlay search box". When users click on the magnifying glass icon, a full-screen overlay with a search input field appears. You can see an example image here: .

Currently, to enter text in this fullscreen search box, users need to click on the placeholder and then type their search query. I want to make it so that the input field automatically reacts and populates with text as soon as a key is pressed down.

I have provided some CSS code for styling purposes, but not sure how to achieve the auto-reactive behavior for the search box input field. Any help or guidance on how to implement this would be greatly appreciated!

Answer №1

There doesn't seem to be a navigation bar with a clickable magnifying glass, but you can create an HTML element that triggers a function to display an overlay.

Here is an example:

<nav>
   <ul>
   <li id="magnifying-glass"></li>
   (...)
</nav>

  <div id="overlay-content">
    <form action="/action_page.php">
      <input id="overlay-input" onblur="this.placeholder = 'Search'" onfocus="this.placeholder = ''" type="text" placeholder="Search" name="search">
    </form>
  </div>

<script>
   document.getElementById("magnifying-glass").addEventListener("click", function(e) {
       e.preventDefault();
       document.getElementById("overlay-content").style.display="block"; //assuming it was "none" before through css
       document.getElementById("overlay-input).focus();
   });
</script>

I typed this in SO's editor so there may be some syntax errors, but the important part is using the .focus() function after displaying the overlay to achieve the desired functionality.

For more information, visit https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/focus

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

Retrieve information from the pouchdb database

After successfully storing data in PouchDB, I'm wondering how to retrieve this information and display it in HTML using AngularJS. var db = new PouchDB('infoDB'); function getData(){ $.getJSON('test.json', function(data) { ...

Passing Information to VueJs Modal Component

I am trying to send data from the first component to a modal in another component. Both components are located in the same place. I need to display the data from the first component in my edit modal form. Below is the code snippet: form.vue: <templat ...

Sending an AJAX request to submit a form and receiving a response

I am in the process of developing a Rails application and I am seeking a way to submit a form using Ajax. This functionality is crucial as I want the form submission to occur without causing a full page reload. Initially, I tried using form_remote_tag but ...

Express JS causing NodeJS error | "Issue with setting headers: Unable to set headers after they have been sent to the client"

As I embark on my journey to learn the fundamentals of API development, I am following a tutorial on YouTube by Ania Kubow. The tutorial utilizes three JavaScript libraries: ExpressJS, Cheerio, and Axios. While I have been able to grasp the concepts being ...

Changing the element tag and flipping escape characters in html entities

My control over the string source is limited, as I can only use html(). However, I am in need of cleaning up the chaos within the source, The goal is to remove all instances of <div class="page"></div>, while retaining its content. The challen ...

I'm having trouble getting this button and icon to align properly. How can I fix this misalignment

Take a look at the HTML code snippet I'm currently working on... <div id="projects"> <H1 class="projects">PROJECTS</H1> <div class="box"></div> <div class="box"></div> <div class="box"></ ...

Implementing experimental decorators and type reconciliation in TypeScript - A step-by-step guide

My basic component includes the following code snippet: import * as React from 'react'; import { withRouter, RouteComponentProps } from 'react-router-dom'; export interface Props { }; @withRouter export default class Movies extends R ...

What is the process for adding JSON object data to an existing JSON file using an HTML input form?

I have a JSON file named "WordMeanings.json" on my computer with the following data: { "WordMeanings": [ { "bangla": "ei je", "example": "Hi there!", "english" ...

Guide to making a sidebar navigation menu on the left using Bootstrap

How can I create a left navbar using Bootstrap? I managed to do it. <nav class="btn-group-vertical float-left navbar"> <button routerLink="product/home" routerLinkActive="is-active" class="btn btn-outline-dark">Home</button> ...

Is it possible to customize the border spacing for individual cells?

My table resembles the one shown in this example I am looking to eliminate the gap between each "Previous" and "Current" cell pairs while still maintaining spacing between rows and other columns. Is there a way to achieve this? ...

How can jQuery grep be used with dual arrays?

Could it be a problem with my syntax, or am I completely off track with my approach here? I'm working with two arrays that store attributes selected by the user. I'm then attempting to filter a JSON file using $.grep() to find pillows that match ...

Is it possible to create a new field in mongoDB from a separate collection without any dependencies?

I manage two different sets of data: profiles and contents The profiles collection is structured as follows: { _id: ObjectId('618ef65e5295ba3132c11111'), blacklist: [ObjectId('618ef65e5295ba3132c33333'), ObjectId('618ef65e5295 ...

divide the information within an HTML table cell

One way I have figured out to populate the cell of a table is by using the following code: {% for row in data %} <td> {{ row[4] }}</td> Now, let's say I have the data "['sugar']:['3642847']:['2020-08-21' ...

JavaScript: Creating Custom IDs for Element Generation

I've been developing a jeopardy-style web application and I have a feature where users can create multiple teams with custom names. HTML <!--Score Boards--> <div id="teamBoards"> <div id="teams"> ...

Assistance Needed with XPATH and CSS for Selenium Automation

Can anyone assist me in finding the appropriate XPATH/CSS locator to extract text from the structure below? <div class="page-header song-wrap"> <div class="art solo-art"> <div class="meta-info"> <h1 class="page-title"> Zehnaseeb I ...

Ways to resolve eslint typedef error when using angular reactive forms with form.value

I am facing an issue with my formGroup and how I initialized it. Whenever I try to retrieve the complete form value using form.value, I encounter an eslint error related to typecasting. userForm = new FormGroup<user>({ name: new FormControl<st ...

The Javascript query is returning an [object Object] data type

I am facing an issue with a JavaScript file that is querying a SharePoint list. Specifically, the Priority drop down in the query result is displaying as [object OBJECT]. I suspect it has something to do with the var query string where I have added the &ap ...

Incorporate the teachings of removing the nullable object key when its value is anything but 'true'

When working with Angular, I have encountered a scenario where my interface includes a nullable boolean property. However, as a developer and maintainer of the system, I know that this property only serves a purpose when it is set to 'true'. Henc ...

Issues with Jquery Autocomplete feature when using an Input Box fetched through Ajax requests

When a user selects an option from the drop-down list, an input box is dynamically added to the page using AJAX. document.getElementById("input_box").innerHTML ="<input id='ProjectName'/>"; However, there seems to be an issue with jQuery ...

Is there a way to automatically update the URL to include $_GET['data'] (media.php?data=123) when a selection is made from a dropdown list?

I'm currently working on a project that involves a website in PHP and a MySQL database. I have successfully linked the two together, and now I have a combobox filled with data from the database. Below is my script for handling the selection: <scr ...