'jQuery On() is not working for $(document)
I am currently in the process of integrating the dropkick.js plugin into my app, but I have run into a few snags. When I change backbone views the events do not work properly and the .live() event associated in dropkick.js just flat out doesn't work at all. Nothing fires. I decided to upgrade this to using the .on() function and got it sort of working (even though it still deletes my url for some reason).
This doesn't work at all:
$(document).on("click", ".dk_toggle", function() {
This only works somewhat:
$(".content").on("click", ".dk_toggle", function() {
Do you know why document doesn't work at all?
My backbone $el is $(".content").
Solution 1:[1]
Instead of document, use body. It basically gives the same behavior.
$('body').on("click", ".dk_toggle", function() {
//....
});
Solution 2:[2]
The first example demonstrates event delegation.
The second example binds the event handler directly to the element. The event will still bubble (unless you prevent that in the handler) but since the handler is bound to the target, you won't see the effects of this process.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Starx |
| Solution 2 | Dipesh Parmar |
