I used the following method to add a click event to id, I was wondering if I can do the same with the class .... I have several elements (which are created in for each loop) and I need to be able to click on them and then the pickup that was pressed ... here is my existing code
$('submit-button').bind('click', submit_click); function submit_click() { alert('I am clicked'); }
I was wondering if there is a way to pass the variable into my function for a click so that I can check the ID? or similar
therefore it
function submit_click(element) {
Any help really appreciated
thanks
EDIT
I tried the following and there is undefined in the "elem" debugging ...
$('.clear').bind('click', clear_click($(this))); function clear_click(elem) { alert(elem.attr("id")); }
WORKING DECISION
I have a working solution, but I donβt quite understand why, I would like to know why it works.
First of all, I tried
$('.clear').bind('click', clear_click($(this)) );
This seemed to work "BUT" when I loaded the page by entering the "clear_click" method without clicking - weird ...
Then I tried this.
$('.clear').bind('click', function() { clear_click($(this)) } );
It works great! But I donβt understand why I should pass the function, and then inside my function call my clear_click.
Can someone explain why 1 works and the other doesn't?
When I need to call a callback function or the like, should I first open the function () and then call the method inside the function?