I'm trying to unbind
/ off
all events of every element inside one element. I tried this solution but it doesn't work. The only thing worked was $(document).add('*').off();
but this is not what I need.
I made a short example, but there are more elements and events.
Why it isn't working this code?
$(document).ready(function() {
$(document).on("click", "#contentWithEvents button", function() {
console.log("Clicked");
});
$(document).on("mouseenter", "#contentWithEvents li div", function() {
console.log("Hovered");
});
$(document).on("click", "#unbind", function() {
contentToBeReplaced = $("#contentWithEvents");
contentToBeReplaced.find("*").each(function() {
$(this).off();
});
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<header></header>
<main id="contentWithEvents">
<button>Click event</button>
<button>Click event</button>
<button>Click event</button>
<ul>
<li><div>Hover event</div></li>
<li><div>Hover event</div></li>
</ul>
</main>
<button id="unbind">Unbind all events</button>
<footer></footer>