Note that there are some explanatory texts on larger screens.

plurals
  1. POJavascript Setting multiple onmouseover events with a for loop
    primarykey
    data
    text
    <p>Sorry, I'm new to javascript so if my coding style is strange please point it out</p> <p>Here's what I have:</p> <pre><code>document.getElementById("img1-container").onmouseover = function(){increaseWidth(1)}; document.getElementById("img2-container").onmouseover = function(){increaseWidth(2)}; document.getElementById("img3-container").onmouseover = function(){increaseWidth(3)}; document.getElementById("img4-container").onmouseover = function(){increaseWidth(4)}; </code></pre> <p>Here's what I'm trying to convert it into:</p> <pre><code>for(var i = 1; i &lt;= numimages; i++){ (function (i) { document.getElementById("img"+i+"-container").onmouseover = function(){increaseWidth(i)}; document.getElementById("img"+i+"-container").onmouseout = decreaseWidth; })(i); } </code></pre> <p>I've read this: <a href="https://stackoverflow.com/questions/8672416/creating-a-loop-from-a-series-of-onmouseover-events">Creating a loop from a series of onMouseOver Events</a></p> <pre><code>var numImages = 7; var totalPercent = 100; var increaseWidthPercent = 40; var decreaseWidthPercent = Math.floor((totalPercent-increaseWidthPercent)/numImages); var originalWidthPercent = Math.floor(totalPercent/numImages); function increaseWidth(imgNum){ document.getElementById("img"+imgNum+"-container").style.width = increaseWidthPercent + "%"; for(var i = 1; i &lt;= numImages; i++){ if(i != imgNum){ document.getElementById("img"+i+"-container").style.width = decreaseWidthPercent + "%"; } } return true; } function decreaseWidth(){ for(var i = 1; i &lt;= numImages; i++){ document.getElementById("img"+i+"-container").style.width = originalWidthPercent + "%"; } return true; } function setImageContainers(){ /*for(var i = 1; i &lt; 8; i++){ document.write("&lt;p&gt;document.getElementById(\"img"+i+"-container\").onmouseover = function(){increaseWidth("+i+")};document.getElementById(\"img"+i+"-container\").onmouseout = decreaseWidth;&lt;/p&gt;"); }*/ document.getElementById("img1-container").onmouseover = function(){increaseWidth(1)};document.getElementById("img1-container").onmouseout = decreaseWidth; document.getElementById("img2-container").onmouseover = function(){increaseWidth(2)};document.getElementById("img2-container").onmouseout = decreaseWidth; document.getElementById("img3-container").onmouseover = function(){increaseWidth(3)};document.getElementById("img3-container").onmouseout = decreaseWidth; document.getElementById("img4-container").onmouseover = function(){increaseWidth(4)};document.getElementById("img4-container").onmouseout = decreaseWidth; document.getElementById("img5-container").onmouseover = function(){increaseWidth(5)};document.getElementById("img5-container").onmouseout = decreaseWidth; document.getElementById("img6-container").onmouseover = function(){increaseWidth(6)};document.getElementById("img6-container").onmouseout = decreaseWidth; document.getElementById("img7-container").onmouseover = function(){increaseWidth(7)};document.getElementById("img7-container").onmouseout = decreaseWidth; } function setImageContainers2(){ for(var i = 1; i &lt;= numimages; i++){ (function (i) { document.getElementById("img"+i+"-container").onmouseover = function(){increaseWidth(i)}; document.getElementById("img"+i+"-container").onmouseout = decreaseWidth; })(i); } } window.onload = setImageContainers2; </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