I have a form where I want to show a popover helpbox on hovering over the info div:
The info div and the popover are as follows (parent div is position: relative
):
<div popovertarget="function-help" class="absolute cursor-help right-1 top-0 bottom-0 m-auto rounded-full border-2 border-cyan-400 text-cyan-400 w-1 h-1 p-2 flex justify-center items-center">i</div>
<div id="function-help" popover>Here's how to input a function:</div>
I know how to do it with a button or input, but how to do it with a div, and on hovering?
How do I show the popover when the user hovers over the info div, and hide it when he leaves?
Thanks in advance!
3
Answers
Ok, thanks to @Sai Manoj's helpful answer, I got that I could use the
mouseenter
andmouseleave
methods of a div to trigger the popover.So here's my code:
Note that you could further style the popover, but you would most probably want to use
position: absolute
(since default isposition: fixed
) andinset: unset
.As per my understanding you are trying to display a tooltip using
div
when user hovers oni
. If that’s the case you can useEventListener
to show/hide div as per your requirementI realized my confusion. The Popover API is an experimental feature that doesn’t have a wide browser compatibility.
My answer allows you to have multiple popovers on the same page.
It uses querySelectorAll to find all of the popovertarget elements. Then it loops through each generating a variable to hold the popover element. Then on mouseover it shows it and on mouseout it hides it.
Personally I would use a plugin that mimics the popover as it will have cross browser compatibility.