Reputation: 25542
I have a script that is looking for a live click event, but I am wanting the click to not apply to certain <a>
tags
Here is my code so far:
$('a:not(.btn-control)').filter(function(index) {
return $(this).attr('href').match(/#|void/);
}).live("click", function() {
var url;
url = $(this).attr('href');
url = url.replace(/^.*#/, '');
$.history.load(url);
return false;
});
Issue
I have site where I have a static player, upon the clicking of a link I am using the HTML5 history functionality to load the new content so that the page never refreshes... to avoid having to go throughout my whole site and add classes to all of the links that should contain this functionality onClick, I am simply trying to filter out those with a #
or a javascript:void(0)
in the href
Question
How do I filter out those links whose href
contains a javascript:void(0)
or a #
?
Working Code
$('a:not(.btn-control)').live("click", function() {
var self, url;
self = $(this);
url = self.attr('href');
if (url.match(/#|void/) === null) {
url = url.replace(/^.*#/, '');
$.history.load(url);
return false;
}
});
Upvotes: 0
Views: 981
Reputation: 75317
The jQuery docs state that:
DOM traversal methods are not supported for finding elements to send to .live(). Rather, the .live() method should always be called directly after a selector, as in the example above.
Instead, you should handle all a
tags, but exit out if you don't need to handle it.
$('a:not(.btn-control)').live("click", function() {
var self = $(this);
if (!self.attr('href').match(/#|void/)) {
return;
};
// else continue and do stuff
var url = self.attr('href');
url = url.replace(/^.*#/, '');
$.history.load(url);
return false;
});
Upvotes: 2