title: How to ssign Variable with div element's name
body: <p>So I got multiple divs with different images embedded. Each one has its unique name attributes. I'm trying to apply the hover effect to each divs by changing the image source. I don't want to write multiple scripts, rather I'm trying to write a just one block of script that would effect every div.</p>
  
  <pre><code>&lt;div id="div1" &gt;
      &lt;img id="img1" name="img1" src="img1_up.jpg" /&gt;
  &lt;/div&gt;
  &lt;div id="div2"&gt;
      &lt;img id="img2" name="img2" src="img2_up.jpg" /&gt;
  &lt;/div&gt;...and so on
  </code></pre>
  
  <p>Now here is the script that I currently have for the rollover effects</p>
  
  <pre><code>&lt;script&gt;
      var var1 = document.getElementById("div1");
      var1.addEventListener("mouseover", changeImage1);
      var1.addEventListener("mouseout", restoreImage1);
  
      function changeImage1() {
          document.getElementById("img1").src = "img1_ro.jpg";
      }
  
      function restoreImage1() {
          document.getElementById("img1").src = "img1_up.jpg";
      }
  
      var var2 = document.getElementById("div2");
      var2.addEventListener("mouseover", changeImage2);
      var2.addEventListener("mouseout", restoreImage2);
  
      function changeImage2() {
          document.getElementById("img2").src = "img2_ro.jpg";
      }
  
      function restoreImage2() {
          document.getElementById("img2").src = "img2_up.jpg";
      }...and so on
  &lt;/script&gt;
  </code></pre>
  
  <p>I would like to use the name attributes from each images to create dynamic code to apply to all images. Here is what I have in mind but not sure the exact way to write it. PLEASE HELP</p>
  
  <pre><code>...
  var dynamicVar = ????
  dynamicVar.addEventListener("mouseover", changeImage();
  dynamicVar.addEventListener("mouseout", restoreImage();
  function changeImage() {
      document.getElementById(dynamicVar).src = dynamicVar + "_ro.jpg";
  }
  function restoreImage() {
      document.getElementById(dynamicVar).src = dynamicVar + "_up.jpg";
  }
  </code></pre>