jQuery: How do I add a click handler to a class and find out which element was clicked?
<p>I have been using the following method for adding a click event to an id, I was wondering if i could do the smae 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>Ok i have the working solution but i don't fully understand why, i woudl 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 enver 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 clal my clear_click..</p>

<p>Can anyone explain why 1 works and the other doesn't?</p>

<p>When ever i need to call a callback function or similar i should first open a function() and then call the method inside the fucntion?</p>

<p>Thanks in advance</p>
