title: How to set onclick functions to multiple elements?
body: <p>I am trying to use javascript to set the onclick property for my links. When the user clicks the link, I want to set a cookie.</p>
  
  <p>Here is the code to set the cookie:</p>
  
  <pre><code>function setCookie(cookieName, cookieValue, cookiePath, cookieExpires){
      cookieValue = escape(cookieValue);
      if (cookieExpires == "") {
        var nowDate = new Date();
        nowDate.setMonth(nowDate.getMonth() + 6);
        cookieExpires = nowDate.toGMTString();
  } if (cookiePath != "") {
    cookiePath = ";Path=" + cookiePath;
  }
  document.cookie = cookieName + "=" + cookieValue + 
    ";expires=" + cookieExpires + cookiePath;
  }
  
  function getCookieValue(cookieName)
  {
  var cookieValue = document.cookie;
  var cookieStartsAt = cookieValue.indexOf(" " + cookieName + "=");
  if (cookieStartsAt == -1)
  {
    cookieStartsAt = cookieValue.indexOf(cookieName + "=");
  }
  if (cookieStartsAt == -1)
  {
    cookieValue = null;
  }
  else
  {
    cookieStartsAt = cookieValue.indexOf("=", cookieStartsAt) + 1;
    var cookieEndsAt = cookieValue.indexOf(";", cookieStartsAt);
    if (cookieEndsAt == -1)
    {
       cookieEndsAt = cookieValue.length;
    }
    cookieValue = unescape(cookieValue.substring(cookieStartsAt,
       cookieEndsAt));
  }
  return cookieValue;
  }
  </code></pre>
  
  <p>And now here is the code to set the onclick function:</p>
  
  <pre><code>var elements = document.getElementsByTagName('a');
     for(var i = 0, len = elements.length; i &lt; len; i++) {
          elements[i].onclick = setCookie("workshop", elements[i].getAttribute("name"), "", "");
     }
  </code></pre>
  
  <p>I would like for the cookie value to be set as the name of the link. For example, if I have a link <code>&lt;a href="#" name="test2"&gt;</code> , I want the cookie to be set to "test2". </p>
  
  <p>However, with this code, the cookie value keeps coming out to be the name attribute of the last &lt; a > tag on the page (the last link). I want each link to have their own onclick function; not all of them to set the cookie to the same value. This way, once the user reaches the next screen, I will know which link they clicked to get there.</p>
  
  <p><strong>ANY HELP IS GREATLY APPRECIATED! THANKS AHEAD!</strong></p>