Have a look at http://jsfiddle.net/60j9hhz7/3/
UPDATED FIDDLE (use this) http://jsfiddle.net/60j9hhz7/10/
html:
<div class="mycontainer">
<a href="#" class="test">test</a>
</div><br/>
<a href="#" class="unbind">doesn't work</a><br />
<a href="#" class="unbind2">works</a><br />
<a href="#" class="unbind3">works too</a>
js:
$(document).on('click', '.test', function (e) {
alert('hello');
});
$(document).on('click', '.unbind', function (e) {
$(document).off('click', '.mycontainer a');
});
$(document).on('click', '.unbind2', function (e) {
$(document).off('click');
});
$(document).on('click', '.unbind3', function (e) {
$(document).off('click', '.test');
});
In the fiddle you'll see that the first off doesn't work (the alert still pops up) while all the others work.
Could somebody explain why this is and if there is a solution? I need to be able to target all links within a certain container but they may have hooked up their click events with different selectors.
Problem is unbind2 is too unspecific and unbind3 too specific
$(document).on('click', 'a', function() { $(this).css('color', 'red'); })it does work