For those looking to create a simple hover menu, JavaScript is not necessary as CSS alone can do the job.
Check out this jsfiddle demonstration showcasing how to utilize CSS for a hover menu.
To address your query directly, you have the option of modifying the CSS properties display
or visibility
using JavaScript like this:
var element = document.getElementById('someElement');
element.style.display = 'none'; // the element is hidden
element.style.display = 'block'; // the element is displayed as a block-level element
element.style.visibility = 'hidden'; // the element is hidden
element.style.visibility = 'visible'; // the element is visible
The distinction between display
and visibility
lies in how you want the invisible element to behave. With the display
property, the element will occupy no space on the page, having zero height and width, while padding or margin won't affect it. Conversely, the visibility
property means that the contents of the element (text, child elements, etc.) are not visible, but the element still occupies space in the DOM, causing other elements to adjust around it. This feature can be useful for hiding/showing content without disrupting the layout as everything becomes visible again.