I am trying to create an event delegation for a dynamically added/removed class. I am creating an event for specific elements that are clicked and don't have that class added to them.
Here's my line of code but it's not working. Is my syntax wrong? If so what would the proper way be to fix this code?
$('.gallery-cell a img').off('click').on('click', ':not(.g-clicked)', function() {
// more code here
});
The second argument in the $selector.on()
is a filter for the decendants of your $selector. Since img
can't have children, you most likely want to do something like this (depending on which item you dynamically add your class):
$('.gallery-cell a').on('click', 'img:not(.g-clicked)', function() {
// more code here
});
or
$('.gallery-cell').on('click', 'a:not(.g-clicked) img', function() {
// more code here
});