What is the best way to implement a focusout or blur event for an entire section in your project?

We have recently added a new feature where, upon clicking on a button, a section appears with input fields and a search button. However, we are facing an issue - when using the tab key to navigate within the section, each element triggers a separate blur event, causing the section to hide prematurely. Our goal is for the entire div section to be hidden only when it goes out of focus completely. Is there a way to achieve this functionality without using jQuery?

function showButton() {
  var x = document.getElementById("searchWithButton");
  x.classList.add("display-none");
  
  var y = document.getElementById("search");
  y.classList.remove("display-none");
}

function showSearchDiv() {
  var x = document.getElementById("searchWithButton");
  x.classList.remove("display-none");
  
  var y = document.getElementById("search");
  y.classList.add("display-none");
  
  var z = document.getElementById("searchInput");
  z.focus();
}
.red-border {
  border: 2px solid red;
  padding: 5px;
 }
 
 .display-none {
  display: none;
  }
<div id="searchWithButton" onfocusout="showButton()" class="red-border display-none">
  <input id="searchInput" type="text">
  <button>Search</button>
</div>
<button id="search" onclick="showSearchDiv()">Search</button>

Answer №1

What is the preferred method for hiding the searchWithButton? I would like to propose a few options: clicking outside, pressing [escape], or moving focus to another element using [tab] (although this may not work consistently).

var x = document.getElementById("searchWithButton");
var y = document.getElementById("search");
var z = document.getElementById("searchInput");

function showButton() {
  x.classList.add("display-none");
  y.classList.remove("display-none");
}

function showSearchDiv() {
  x.classList.remove("display-none");
  y.classList.add("display-none");
  z.focus();
}

window.addEventListener('keydown', function(ev) {
  if (ev.keyCode==27) {
    showButton();
  }
})

window.addEventListener('mousedown', function(ev) {
  if (!ev.target.closest("#searchWithButton")) {
    showButton()
  }
})

document.querySelectorAll(".get-focus").forEach(function(item) {
  item.addEventListener('focus', showButton)
})
.red-border {
  border: 2px solid red;
  padding: 5px;
}

.display-none {
  display: none;
}
before: <input type="text" name="before" class="get-focus">
<br><br>

<div id="searchWithButton" class="red-border display-none">
  <input id="searchInput" type="text">
  <button>Search</button>
</div>
<button id="search" onclick="showSearchDiv()">Search</button>

<br><br> after: <input type="text" name="after" class="get-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

The total height of an HTML element, which takes into account the margin of the element itself

Is there a way to accurately calculate the height of an element including margins, even when dealing with child elements that have larger margins than their parents? HTMLElement.offsetHeight provides the height excluding margin, while this function from ...

Exploring the capabilities of this class in Jquery attribute manipulation

Presented below is a piece of code written in HTML and jQuery HTML <div id="myForm" class="myForm row"> <div class="form-group row"> <div class="col-lg-12"> <div class="col-md-4"> <label& ...

Dividing a select option

Looking to transform a single select element into multiple select elements using the separator "/" Here is an example of the original code: <select> <option value="1234">Type 1 / Black</option> <option value="5678">Type 2 / White& ...

Validating date inputs with ng-change in AngularJS

I am currently utilizing AngularJS along with AngularJS bootstrap within my webpage. One of the components I have is a date picker directive that is structured like this: <div class="form-group {{dateStatus.class}}"> <p class="input-g ...

What is the best way to customize the small square area found between the vertical and horizontal scrollbars?

Please take a look at the images provided below: https://i.stack.imgur.com/QVHfh.jpghttps://i.stack.imgur.com/cubd8.jpg Whenever both vertical and horizontal scrollbars intersect, it results in a square space. This phenomenon occurs not just within the b ...

Navigating to a URL parameter outside of the <router-outlet>

Currently, I am developing a simulated e-commerce platform using Angular 11. The structure of my app.component.html file is outlined below: <mat-toolbar> <mat-toolbar-row> <!-- Various buttons and links --> <button *ngIf=&quo ...

Retrieve the element reference for components in the template instead of the component reference

I am attempting to obtain the container element ref for a kendo-maskedtextbox in order to properly anchor a hover component to it. Unfortunately, I am only able to get the component's instance when using #cprNrAnchor. Is there a way to access the cont ...

Eliminate redundant data by utilizing oData to streamline information

I'm trying to clean up my data and eliminate duplicates using oDATA. Currently, I am using !summary=Name in my query, however it's not creating groups and providing the results as expected. Below is my query: http://localhost:12585/OData.svc/Med ...

What is the maximum image size that can be displayed on an HTML canvas?

Is there a limit on the size of an image that can be rendered into an HTML5 canvas? I have noticed that images larger than 5mb are not appearing when converted to a DataURL. For example, a 10mb jpg file results in a black screen when opened as a dataURI. ...

Tips for adjusting the appearance of Material Cards in Angular 6

html: <mat-card class="custom-matcard-shape"></mat-card> css: .custom-matcard-shape { background-color: black; height: 400px; margin-top: 10px; border-radius: 25px; } I am looking to create a unique shape for my mat-card element. ...

Make google maps InfoWindow wider than 700px

To display the InfoWindow content at a size of 840px x 660px, I need to adjust the MaxWidth property when creating the google.maps.InfoWindow object. (For example: new google.maps.InfoWindow({ content: some_text, maxWidth: 840});) However, despite settin ...

Exploring Problems with jQuery FancyBox 2.0: Troubles with Helpers (Buttons and Thumbnails)

Despite meticulously following the instructions for the updated fancybox version 2, I am unable to activate helpers. The issue mystifies me as I cannot pinpoint where I may have made an error. Here is my current code: HTML: <div id="disegni" style="d ...

"Effortless alignment: Centralizing CSS styling across the entire

I need help placing a loading spinner in the center of my screen, ensuring it works on both mobile and desktop devices. Here is my current CSS code: .spinner { width: 100px; height: 100px; position:absolute; top: 50%; left: 50%; margin: 100px ...

Positioning social media logos at the center

Looking for advice on how to center my social media icons horizontally and make them collapse vertically when the screen size changes. I currently have no CSS applied to the icons below. Any suggestions or tips on what CSS properties I can use to achieve ...

Unable to modify date or time in react-datetime package

Upon clicking the input box and entering a valid value, the input will either clear or be assigned a random date and time. This feature allows for the selection of a date or time using the mouse. Typing in the input will clear it or assign random values. ...

Refresh the controller variables displayed in the view

I'm currently working on a rails application and looking to refresh/reload the index method of my controller without refreshing the view. For example, here is my index method: def index @title = params[:title] end And the corresponding html.e ...

Having trouble setting the backgroundImage in React?

Hi there! I'm in the process of crafting my portfolio website. My goal is to have a captivating background image on the main page, and once users navigate to other sections of the portfolio, I'd like the background to switch to a solid color. Alt ...

Merging Zend Framework with Angular for web development purposes

Is there a comprehensive guide available for integrating Zend Framework 3 (or ZF2) with Angular? I am looking to completely replace jQuery. Specifically, I am seeking advice on the recommended application structure, the ideal location to store Angular com ...

Substitutions not functional when using SendGrid in conjunction with Firebase functions

I'm encountering difficulties when trying to include substitutions data in emails sent from Sendgrid using Firebase Cloud Functions. Here's my function exports.firestoreEmail = functions.firestore .document('users/{id}') .onCreate(s ...

I am having trouble with the hover feature on the image tag. Does anyone know how to troubleshoot this issue?

h1{ color:red; font-size: 100px; } img :hover { background-color: gold; } .bacon{ background-color: green; } .broccoli{ background-color: red; } /* .circular{ border-radius: 100%; } */ #heading{ background-color: aquamarine; ...