How can I change this to where when I hover the pointer outside the button and search-list div the search-list disappears? As it is, the search-list cannot be hidden when hovering outside the div or button.
I realize I likely need to change the javascript code, but I’m having trouble understanding how to do this. Thanks.
function ListToggle() {
var L = document.getElementById("search-list");
L.style.display = "block"; // <-- Set it to block
}
#search-list {
width: 150px;
height: 150px;
background-color: aquamarine;
}
<button onclick="ListToggle()">Tests</button>
<div id="search-list" style="display:none">
search-list
</div>
3
Answers
EDIT: I just found out you want to hide the list when the mouse exits which @RoryMcCrossan already showed correctly. I just showed how to toggle the list using the same button. I will keep this answer anyway as it might give you some ideas.
There are plenty of ways to handle such a thing. But all of them need the
if
statement. I will show you 3 examples.1. Check the current styles using
style
property:You can easily check the current style of the element and tell JS that if it is
block
set it tonone
or vice versa:2. outer variable
You can define a variable outside of the function and set it the
true
orfalse
which describes the state of thesearchList
.3. Dataset (mix of CSS, JS, and HTML)
If you are going to have many elements with the same functionality. You can do it using this approach. Here, we can set a dataset to our elements and switch the styles based on it using CSS. (read more about the dataset in Mozilla documentation) I will show you how:
You can also set the default state using HTML (optional in this example):
To do what you require you can hook a
mousemove
event to the document. In that event handler you can interrogate theevent.target
to see if it’s the button trigger or the search list, if it’s not, then hide the content.Note the use of
addEventListener()
in the JS instead ofonX
attributes in the HTML. The latter is bad practice and should be avoided where possible. In addition, note that I moved the CSS styling in to the stylesheet. Don’t put CSS in your HTML.That being said, it would be possible to achieve your desired affect much more simply by using CSS alone, assuming that having the list appear when the mouse hovers the trigger button meets your requirements:
It is much better not to use inline styles or scripts, as most typical
content-security-policy
‘s will ban both for being unsafe. You should do styles on a stylesheet, and code in a JS file.