title: How do I add a click handler to a class and find out which element was clicked?
body: <p>I have been using the following method for adding a click event to an id, I was wondering if I could do the same with a class.... I have a number of items (which are created in a for each loop) and I need to be able to click them and then pickup which was clicked... here is my existing code</p>
  
  <pre><code>$('submit-button').bind('click', submit_click);
  
  
  function submit_click() {
      alert('I am clicked');
  }
  </code></pre>
  
  <p>I was wondering if there is some way to pass in a variable into my function for the click so i can check the ID?? or similar</p>
  
  <p>hence this</p>
  
  <pre><code>function submit_click(element) { // notice element
      alert(element + ' clicked');
  }
  </code></pre>
  
  <p>Any help really appreciated</p>
  
  <p>Thank you</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I have tried the following and in debug "elem" is undefined...</p>
  
  <pre><code>$('.clear').bind('click', clear_click($(this)));
  
     function clear_click(elem) 
     {
          alert(elem.attr("id"));
     }
  </code></pre>
  
  <p><strong>WORKING SOLUTION</strong></p>
  
  <p>I have the working solution but I don't fully understand why, I would love to know why it works..</p>
  
  <p>First of all I tried</p>
  
  <pre><code> $('.clear').bind('click', clear_click($(this)) );
  </code></pre>
  
  <p>This seemed to work "BUT" when I loaded the page it enter the "clear_click" method without being clicked - strange...</p>
  
  <p>Then I tried this..</p>
  
  <pre><code> $('.clear').bind('click', function() { clear_click($(this)) } );
  </code></pre>
  
  <p>This works great! But I don't understand why I must pass a function and then within this function call my clear_click.</p>
  
  <p>Can anyone explain why 1 works and the other doesn't?</p>
  
  <p>Whenever I need to call a callback function or similar I should first open a function() and then call the method inside the function?</p>