Reputation: 78
I have this function sideNav that is triggered by both clicking on a subnav item
$(".side-nav ul li a").click(function(event) {
$(document).ready(sideNav());
});
if(window.location.hash) {
$(document).ready(sideNav());
}
I am trying to unbind sideNav when a certain element is clicked
$(".side-nav .page-header h1 span").click(function(event) {
}).unbind('ready',sideNav);
I have tried using .live() and .die(), but these wont work on the hashtag if statement.
Any help would be appreciated. Thank you.
Upvotes: 0
Views: 549
Reputation: 318182
on() and off() would be the preferred way to attach and detach event handlers.
Also, wrap the code in document ready to make sure the DOM has loaded when you execute the code, do not wrap every function in it's own document ready function.
When you referrence a function, you type just the function name "sideNav
", if you add the parenthesis, like "sideNav()
" you execute that function right away and pass back the value of that function, and the default value unless something else is returned is "undefined", and when adding the parenthesis your document ready functions aren't working at all.
$(document).ready(function() {
$(".side-nav ul li a").on('click', sideNav);
$(".side-nav .page-header h1 span").on('click', function() {
$(".side-nav ul li a").off('click');
});
});
Upvotes: 0
Reputation: 17348
Look into jQuery bind() and unbind().
Do to change your click(), you could do:
$(".side-nav ul li a").bind('click', function(event) {
$(document).ready(sideNav());
});
$(".side-nav ul li a").unbind('click');
Upvotes: 2