Reputation: 712
My code is for a working model of a knee. In order to load each view of the knee dynamically, and get rid of the hash in my URL, I used the pushState method, which includes a .click()
event. So basically i have two categories, and when you click on one of the options from the categories, the application loads a description of that part of the knee. But for some reason, i can click the category, which loads dynamically, but when I click one of the options in the category, the page refreshes. After that refresh, each option in the category loads dynamically. I'm wondering if my .click()
events are clashing, or possibly causing a refresh for some reason. And if they are, I need to be able to join the two in one function without disrupting the pushState method. Any thoughts? Here's the code I think may be clashing:
$(function() {
$('.knee-navigation a').click(function() {
$('.knee-navigation a').removeClass();
$(this).addClass('current_cat');
if ($('.maladies-label a').hasClass('current_cat')) {
$(".term-list").load("maladies-menu.php ul");
} else {
$(".term-list").load("anatomy-menu.php ul");
}
});
});
$(function() {
var main = $("main");
$("a, area").click(function() {
var href = $(this).attr("href");
history.pushState('', '', href);
// load the content
loadContent(href);
return false;
});
});
Help!
Upvotes: 1
Views: 54
Reputation: 252
If you'd like to remove the click event from a given element you can use jQuery's unbind
function (https://api.jquery.com/unbind/)
It would look like this:
$("a, area").unbind("click");
Upvotes: 1
Reputation: 17952
The default action for some elements (eg: button', 'a', etc) cause the page to go elsewhere. You can prevent this by making your handlers
preventDefaulton the
event` object, like so:
$('a, area').on('click', function (e) {
// do stuffs
e.preventDefault();
});
Upvotes: 1