title: Keep parameter value assigning Event Listener on loop
body: <p>I have a <code>while</code> loop that attaches an Event Listener (<code>SelectRua()</code>) to a <code>div</code>.</p>
  
  <p>I was needing the <code>div</code> inner text inside <code>SelectRua()</code> method, and since the <code>div</code> is what's calling the Event Listener, this worked fine:</p>
  
  <pre><code>$('#div' + i).on("click", function () { SelectRua(this) })
  </code></pre>
  
  <p>And like so i easily accessed the <code>div</code> attributes when it is clicked.</p>
  
  <p>But now i want to assign an object (variable) that will no longer be available when the click on the <code>div</code> occurs:</p>
  
  <p>As i saw in <a href="https://stackoverflow.com/questions/8909652/adding-click-event-listeners-in-loop">adding &#39;click&#39; event listeners in loop</a> and <a href="https://stackoverflow.com/questions/750486/javascript-closure-inside-loops-simple-practical-example">JavaScript closure inside loops – simple practical example</a> i was able to reach this code, after many tries:</p>
  
  <pre><code>while (morada[i]) {
  $('#div' + i).on("click", function (morada) {
      return function (morada) { SelectRua(morada[i]) }
      }(morada))
  }
  </code></pre>
  
  <p>When the <code>div</code> is clicked, the <code>SelectRua()</code> method is called, but the parameter is <code>undefined</code>, although i put it everywhere in this example. </p>
  
  <p>So my question is, how can i set the value of the parameter when it's assigned to the Event Listener the be used when the event is fired?</p>