Hide the selection box when hovering over the Div

Looking for a quick solution. I want the option drop down to close when another div element is hovered over.

First, open the drop down and hover over the red element on the right side. When hovering over the red element, I would like the drop down to close.

Thank you in advance.

select {
  width: 200px;
  display: inline-block;
}

div {
  width: 50px;
  height: 50px;
  background: red;
  display: inline-block;
}
<select>
  <option>1</option>
  <option>2</option>
  <option>3</option>
  <option>4</option>
  <option>5</option>
</select>

<div></div>

Answer №1

When the div is hovered over, you can easily trigger the blur() event on the select element like this:

Using mouseenter:

let containerDiv = document.getElementById("containerDiv"); 
let selection = document.getElementById("selection"); 
containerDiv.addEventListener("mouseenter", function( event ) {   
  selection.blur();
}, false);
select{width:200px;display:inline-block}
#containerDiv{width:50px;height:50px;background:red;display:inline-block}
<select id="selection">
  <option>1</option>
  <option>2</option>
  <option>3</option>
  <option>4</option>
  <option>5</option>
</select>

<div id="containerDiv"></div>

Using onmouseover:

let containerDiv = document.getElementById("containerDiv");
let selection = document.getElementById("selection");
containerDiv.onmouseover = function() {
  selection.blur();
};
select{width:200px;display:inline-block}
#containerDiv{width:50px;height:50px;background:red;display:inline-block}
<select id="selection">
  <option>1</option>
  <option>2</option>
  <option>3</option>
  <option>4</option>
  <option>5</option>
</select>

<div id="containerDiv"></div>

Using jQuery .hover():

$('#containerDiv').hover(function() {
  $('#selection').blur();
});
select{width:200px;display:inline-block}
#containerDiv{width:50px;height:50px;background:red;display:inline-block}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select id="selection">
  <option>1</option>
  <option>2</option>
  <option>3</option>
  <option>4</option>
  <option>5</option>
</select>

<div id="containerDiv"></div>

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

How can I transform this in JavaScript so that it returns a Promise?

Currently, I am trying to implement code that I found in a SaaS's example. The goal is to retrieve the correct URL for a specific action. The sample code provided by the SaaS looks like this, but I would like to modify it so that it returns a Promise ...

Having trouble getting the hover effect to work when selecting a different section of the SVG

In my SVG checkbox design, I have a circle element surrounding a polyline element (which acts as the checkmark). The boundaries of the icon extend beyond the circle, causing hover styles to trigger even when hovering outside the circle. I want to change st ...

Looking to find top-notch keywords that stand out from the rest?

My chosen keyword is s='young girl jumping' function selfreplace(s) { var words = ['man', 'jumping']; var re = new RegExp('\\b(' + words.join('|') + ')\\b', 'g&a ...

Consider utilizing divs in a similar manner to tables

I typically use tables to align images and text, but they don't work well on mobile devices. I'd like to switch to using CSS and DIVs instead, even though I have no experience with them. My goal is to center three pictures with text above each o ...

Unable to set height of images using jQuery following an ajax request

I have a function that determines the highest image height to set as the minimum height of the image container element, specifically a figure. Initially, the function works well on window load and when the window is resized. However, after an ajax call lo ...

Steps to incorporate a session into an HTML login form

Need assistance with adding a session to my HTML form connected to MySQL database. The database updates whenever the page is reloaded or when a failed submission occurs. Can anyone provide a solution for this? <body> <?php // define variables a ...

The MySQL database will need to contain an HTML form with specific styling attributes

I have been experimenting with creating an HTML form that allows users to add style attributes to their posts, such as Bold, Center, Font-Size, Link, Image, and more. However, my English skills are not the best, so I apologize if I am not explaining my que ...

Dealing with multiple v-on:click events in Vue.js

Can I add multiple v-on:click events to the same element? I want to toggle a navigation menu and trigger a CSS animation on the element that toggles the navigation. <template> <div> <nav v-if="visible"> <ul&g ...

Navigating between two Vue2 applications

I currently have two applications, app1 and app2. App1 serves as a website at www.wxample.com with a button. My goal is to implement code in app2 that allows me to click the button on app1 and be redirected to the Login Panel in app2 for the workers module ...

Parsing XML using jQuery AJAX without the need for the find method

I have been experimenting with implementing ajax in our webservices to optimize the loading speed of certain pages. I have managed to create a working sample, but I am looking for a way to make this process more efficient for use across multiple pages with ...

Integrate payment form in Ajax with square pos card option

Looking for help with integrating a Square payment form on my website. I've followed the documentation provided at . When embedding the form on a standalone page, everything works perfectly. However, when trying to load the form scripts via ajax, I en ...

Issue: Importing an ES Module in React-gauge-chart causing error in NextJs

an error appears indicating the following: When attempting to render the package component, an issue arises with the require() function used on ES Module /node_modules/d3/src/index.js from /node_modules/react-gauge-chart/dist/GaugeChart/index.js. The s ...

Troubleshooting Div Element Width in CSS Not Functioning

When working on a form design, I encountered an issue where the text labels were not aligning to the same width. Despite setting the width, it didn't seem to work as expected: .form-label { display:inline; width: 160px; } <div class="form-label ...

What is the reason for having two plugin declarations within the galleriffic.js file?

I am currently working on enhancing the functionality of galleriffic.js by implementing a feature that will update a <div> element with text content as images are being changed. However, I am facing some challenges understanding the code. What perpl ...

What is the process for importing module functions in Svelte when utilizing npm?

I'm having trouble understanding how to properly use imports in Svelte with npm. I have a Svelte component that I can't seem to get working. Uncommenting "//import { Configuration, OpenAIApi } from "openai";" leads to the error message: [!] (plug ...

Response from a Clean jQuery Web Service

I've been seeing a lot of code examples calling web services that return JSON data, but they tend to involve back-end languages like PHP. Does anyone know of a tutorial for a jQuery-only solution? For example, setting up div tags with IDs, then direct ...

The reliability of jQuery posting varies

My current setup involves using jQuery to send data to an external source with the code snippet provided below. As part of the process, I'm also ensuring that all input fields are filled out correctly. However, there has been a strange issue where the ...

Producing identical sets of 2 random hues for a pair of distinct items

I am trying to figure out a way to assign the same randomly generated color to two separate objects. Does anyone have any suggestions on how I could link these two objects so that they always have matching colors, refreshed whenever a condition is met? T ...

Is there a way to create an HTML select element where each option has a unique background color, and will display properly

I want to create multiple HTML select elements with unique background colors for each option: <select runat="server" id="select"> <option value="A">White</option> <option value="B">Red</option> <option value="C">Yellow& ...

"Exploring the relationship between Javascript objects and the '

function createNewRobot(robotIdentifier) { this.id = robotIdentifier; this.components = new Array(); this.gatherComponents = function() { $.getJSON('specific/url', function(data) { for(index in data.responses) { this.part ...