Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating a loop from a series of onMouseOver Events
    text
    copied!<p>how can I create a loop out of this function.</p> <pre><code>window.onload = function makeHalo() { document.getElementById("d1").onmouseover = function() { this.id ="d1On"; this.className="hover"; document.getElementById("menu1").style.color="#6DC5E6"; }; document.getElementById("menu1").onmouseover = function() { this.style.color="#6DC5E6"; document.getElementById("d1").className="hover"; document.getElementById("d1").id="d1On"; }; document.getElementById("d1").onmouseout = function() { this.id ="d1"; this.className=""; document.getElementById("menu1").style.color="#FFFFFF"; }; document.getElementById("menu1").onmouseout = function() { this.style.color="#FFFFFF"; document.getElementById("d1On").className=""; document.getElementById("d1On").id="d1"; }; document.getElementById("d2").onmouseover = function() { this.id ="d2On"; this.className="hover"; document.getElementById("menu2").style.color="#6DC5E6"; }; document.getElementById("menu2").onmouseover = function() { this.style.color="#6DC5E6"; document.getElementById("d2").className="hover"; document.getElementById("d2").id="d2On"; }; document.getElementById("d2").onmouseout = function() { this.id ="d2"; this.className=""; document.getElementById("menu2").style.color="#FFFFFF"; }; document.getElementById("menu2").onmouseout = function() { this.style.color="#FFFFFF"; document.getElementById("d2On").className=""; document.getElementById("d2On").id="d2"; }; } </code></pre> <p>The function pretty much learns the ID of an image when its hovered, changes the ID of that element, adds a class to the element, and changes the color of another element</p> <p>The second part learns the ID of a list item when its hovered, changes its color, and changes the ID of the other image element and adds a class to that element as well. </p> <p>The onmouseout simply resets everything. </p> <p>On the actual HTML page, it is a menu page with lists. Below there a continent map, which is a background image. When you hover over a list item, it swaps out a point on a map with another picture for an indicator. You can also hover the points on the map to change the color of the links on the lists. </p> <p>I tried doing something like this, but the loop only goes to the last iteration for some of the elements. The links change color fine, but it will only swap the picture for "d43" regardless of what link I hover over. </p> <pre><code>window.onload = function makeHalo() { var i = 1; for (i=1; i&lt;44; i++) { var menu = "menu"+i; var d = "d"+i; var On = "d"+i+"On"; document.getElementById(d).onmouseover = function() { this.id = On; this.className="hover"; document.getElementById(menu).style.color="#6DC5E6"; }; document.getElementById(menu).onmouseover = function() { this.style.color="#6DC5E6"; document.getElementById(d).className="hover"; document.getElementById(d).id=On; }; document.getElementById(d).onmouseout = function() { this.id = d; this.className=""; document.getElementById(menu).style.color="#FFFFFF"; }; document.getElementById(menu).onmouseout = function() { this.style.color="#FFFFFF"; document.getElementById(On).className=""; document.getElementById(On).id=d; }; } } </code></pre> <p>Any help will be greatly appreciated. </p> <p>Thanks. </p>
 

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