title: How to add EventListeners with JavaScript, to a varying number of elements the server returns
body: <p>How can i add EventListeners with JQuery/JavaScript to a varying number of elements the server returns? </p>
  
  <p>The server uses a Mustache-Template engine to generate the elements: </p>
  
  <pre><code>...
  {{#resource.recordEntries}}
  &lt;form action="http://x/{{entryId}}/" method="post"&gt;
      &lt;textarea id="inputArea{{entryId}}" class="inputArea"&gt;{{description}}&lt;/textarea&gt;
      &lt;button id="saveEntry{{entryId}}" class="saveEntry" type="submit"&gt;Save&lt;/button&gt;
  &lt;/form&gt;
  {{/resource.recordEntries}}
  ...
  </code></pre>
  
  <p>For example here it returns 2 textareas and 2 buttons:</p>
  
  <pre><code>&lt;textarea id="inputArea1" class="inputArea"&gt;Desciription Area1&lt;/textarea&gt;
  &lt;button id="saveEntry1" class="saveEntry" type="submit"&gt;Save Area1&lt;/button&gt;
  
  &lt;textarea id="inputArea2" class="inputArea"&gt;Desciription Area2&lt;/textarea&gt;
  &lt;button id="saveEntry2" class="saveEntry" type="submit"&gt;Save Area2&lt;/button&gt;
  </code></pre>
  
  <p>The critical part is the registration of the EventListeners. I could do it in a static way like this for every element, but i don't know how many elements the server returns:</p>
  
  <pre><code>$("button[id=saveEntry1]").css({"display": "none"});
  document.getElementById("inputArea1").addEventListener("input", function() {
      $("button[id=saveEntry1]").css({"display": "block"});
  }, false);
  document.getElementById("saveEntry1").addEventListener('click', function() {
    $("button[id=saveEntry1]").css({"display": "none"});  
  }, false);
  </code></pre>
  
  <p>In this <a href="http://jsfiddle.net/martin_martin/bfkwhbsy/" rel="nofollow">code example</a> I register EventListeners to 3 textareas and buttons in a static way. 
  What changes are necessary, that the code works more dynamic? So that it still works even if the server returns more than 3 elements.</p>
  
  <p>Thanks a lot!
  -martin.martin</p>