Hi there,
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
$('submit-button').bind('click', submit_click);
function submit_click() {
alert('I am clicked');
}
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
hence this
function submit_click(element) { // notice element
alert(element + ' clicked');
}
Any help really appreciated
Thank you
EDIT
I have tried the following and in debug "elem" is undefined...
$('.clear').bind('click', clear_click($(this)));
function clear_click(elem)
{
alert(elem.attr("id"));
}
WORKING SOLUTION
Ok i have the working solution but i don't fully understand why, i woudl love 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 it enver the "clear_click" method without being clicked - strange...
Then i tried this..
$('.clear').bind('click', function() { clear_click($(this)) } );
This works great! but i don't understand why i must pass a function and then within this function clal my clear_click..
Can anyone explain why 1 works and the other doesn't?
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?
Thanks in advance