Jquery adding events
Posted
by
JBone
on Stack Overflow
See other posts from Stack Overflow
or by JBone
Published on 2012-04-12T17:22:41Z
Indexed on
2012/04/12
17:29 UTC
Read the original article
Hit count: 138
JavaScript
|jQuery
I want to add an event handler to some dynamically added elements. I simplified my problem into the basic code below. I am using the new JQuery 1.7 on feature to say "hey for all labels in the CancelSurvey id element call this notify function when they are clicked."
function notify(event) {
console.log(event.data.name);
}
$('#CancelSurvey').on('click', 'label', { name: $(this).attr("id") }, notify);
I want to pass the id of the label as parameter "name". When I try to alert this it is undefined (they are defined in the html created). I believe that using the $(this) is not referencing the label selector in this case. It actually seems to be referencing the document itself. Any ideas on how to accomplish this?
© Stack Overflow or respective owner