title: js: jquery - Each button click change attr in element
body: <p>I want to use buttons (stored in var buttons) to change various attribute like "href" inside a hyperlink. The code works however, I would like to short it a little (by using "for" loop perhaps?). </p>
  
  <p>Everything after 24 line in js.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>$(document).ready(function() {
    console.log("lauched\n");
  
    //vars
   var buttons = [];
    var hrefs = [];
    var imageextension = ".png";
    var aselector = $("#link");
    var time = 500;
    
    console.log("link storred:");
    console.log(aselector);
   
    //var assignment using for loop
    for (var i=0; i &lt;= 2; ++i){
    buttons[i] = $("#akapit"+i);
    hrefs[i] = "img/logo" +i+ imageextension;
    //outputs
   console.log("Button storred:");
    console.log(buttons[i]);
    console.log("href storred:");
    console.log(hrefs[i]); 
    }
    
    // it works but how to short this? //
    
     buttons[0].click(function () {
       aselector.hide(0).fadeIn(time).attr("href", hrefs[0]);
     });
     buttons[1].click(function () {
       aselector.hide(0).fadeIn(time).attr("href", hrefs[1]);
     });
     buttons[2].click(function () {
       aselector.hide(0).fadeIn(time).attr("href", hrefs[2]);
     });
    
  });</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"&gt;&lt;/script&gt;
  &lt;html&gt;
  
    &lt;head&gt;
  
      &lt;body&gt;
  
        &lt;span&gt;Run console to see outputs&lt;/span&gt;&lt;br&gt;
  
        &lt;button id="akapit0" class="buttons"&gt;0&lt;/button&gt;
        &lt;button id="akapit1" class="buttons"&gt;1&lt;/button&gt;
        &lt;button id="akapit2" class="buttons"&gt;2&lt;/button&gt;
  
        &lt;a id="link" href="asdasdasd.com"&gt;LINK&lt;/a&gt;
  
      &lt;/body&gt;
    &lt;/head&gt;
  
  &lt;/html&gt;</code></pre>
  </div>
  </div>
  </p>