Reputation: 1937
Why this code is not working?
$("div.class ul li").on("click","a",function(){
alert("A");
});
But this works fine...
$("body").on("click","div.class ul li a",function(){
alert("A");
});
Upvotes: 0
Views: 109
Reputation: 5231
Presumably your code is dynamically inserting li
elements after the page has loaded. For event delegation to work you'll want to bind to the nearest element (parent, grandparent, etc) which exists when the DOM is ready
. The ul
perhaps?
$("div.class ul").on("click","li a",function(){
alert("A");
});
Upvotes: 0
Reputation: 30105
I suppose that your div.class ul li
is dynamically created. You need to call on
function on some existing element (closest parent for example). This is why it works when called on body
.
Upvotes: 2