I'm using event.stopPropagation()
, but I am having problems using .live()
handler on the parent.
//does not work
$("#testlink").live({
click: function(event)
{
alert('testlink');
}
});
//works great!
$("#testlink").click(function() {
alert('testlink');
});
I have to use .live()
because the content is loaded with AJAX.
Can someone help me with this problem?
How can i use more classes or ids like this?
$('#div1', '#div2', '#div3').on('click', 'a.testlink', function(){
alert('testlink');
});
How is this possible?
Use on() (as @techfoobar mentioned, live() is deprecated in version 1.7 and removed as of version 1.9) and event delegation if the content is dynamically generated:
$('#container').on('click', '#testlink', function(){
alert('testlink');
});
Where #container
is an element containing the dynamically loaded #testLink
element (you could use document
, but using a container element here reduces the search scope and increases efficiency).