Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to make a JavaScript menu more functional
    primarykey
    data
    text
    <p>I wonder simply wich way is the best practice to make this menu dynamic. I dont want to make a function for each Element. Should I push them into an Array then loop threw them? All answer and thoughts are welcome.</p> <pre><code> &lt;div id="nav"&gt; &lt;div id="button1"&gt;&lt;/div&gt; &lt;div id="button2"&gt;&lt;/div&gt; &lt;div id="button3"&gt;&lt;/div&gt; &lt;div id="button4"&gt;&lt;/div&gt; &lt;/div&gt; &lt;div id="navHover"&gt; &lt;div id="hoverButton1"&gt;&lt;/div&gt; &lt;div id="hoverButton2"&gt;&lt;/div&gt; &lt;div id="hoverButton3"&gt;&lt;/div&gt; &lt;div id="hoverButton4"&gt;&lt;/div&gt; &lt;/div&gt; &lt;script type="text/javascript"&gt; var buttonOne = document.getElementById('button1'); var buttonOneHover = document.getElementById('hoverButton1'); buttonOne.addEventListener('mouseover', buttonOneBlock, false); buttonOne.addEventListener('mouseout', buttonOneNone, false); buttonOneHover.addEventListener('mouseover', buttonOneBlock, false); buttonOneHover.addEventListener('mouseout', buttonOneNone, false); function buttonOneBlock() { var buttonOneHover = document.getElementById('hoverButton1'); buttonOneHover.style.display = 'block'; } function buttonOneNone() { var buttonOneHover = document.getElementById('hoverButton1'); buttonOneHover.style.display = 'none'; } &lt;/script&gt; #nav { width: 960px; height: 20px; background-color: white; margin: auto; } #button1, #button2, #button3, #button4 { width: 100px; height: 20px; background-color: red; float: left; margin-left: 10px; } #navHover { width: 960px; height: 20px; background-color: white; margin: auto; } #hoverButton1, #hoverButton2, #hoverButton3, #hoverButton4 { width: 100px; height: 20px; background-color: orange; float: left; margin-left: 10px; display: none; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload