I am having dynamically creating remove label. All have same class but different Ids. I want to get the id when I click a label. I used $(.class).click
function but it didn't work. I can't use onclick
function becau开发者_Go百科se $(this).attr('id')
is not working in IE 8.
Please give me a solution.
Thanks.
.click doesn't work on elements that are created later dynamically. therefore use .live()
$('.class').live('click', function(){ alert(this.id); });
Update:
http://api.jquery.com/live/
As of jQuery 1.7, the .live() method is deprecated. Use .on() to attach event handlers.
The above example using .on()
$('body').on('click','.class',function(){ alert(this.id); });
$(".class").click(function(){alert(this.id)});
Try something like this, using the event argument that gets passed to the click handler function instead of this
.
$('label.foo').click(function(event) {console.log($(event.target).attr('id'))});
精彩评论