Reputation: 568
I have the following in my (document).ready function:
replace_fav_url();
and the jQuery function:
function replace_fav_url(){
$j('a.fav').click(function(e) {
$j.post($j(this).attr('href'));
e.preventDefault();
});
}
Everything works good with the exception that function seems to be called twice? 1 click two function calls? Not sure what is going on here?
EDIT:
FULL application.js file:
var $j = jQuery.noConflict();
//Append javascritp header requests
$j.ajaxSetup({
'beforeSend': function(xhr) {xhr.setRequestHeader("Accept", "text/javascript")}
});
$j(document).ready(function() {
$j(document).ajaxSend(function(event, request, settings) {
if (settings.type == 'GET') return;
if (typeof(AUTH_TOKEN) == "undefined") return;
settings.data = settings.data || "";
settings.data += (settings.data ? "&" : "") + "authenticity_token=" + encodeURIComponent(AUTH_TOKEN);
});
replace_vote_urls();
replace_fav_url();
});
function replace_vote_urls(){
$j('a.vote').click(function(e) {
$j.post($j(this).attr('href'));
e.preventDefault();
});
}
function replace_fav_url(){
$j('a.fav').click(function(e) {
$j.post($j(this).attr('href'));
e.preventDefault();
});
}
Upvotes: 0
Views: 1315
Reputation: 86473
I'm only guessing this is the problem, but if you call the replace_fav_url()
function more than once, it will bind an additional click event to a.fav
. And will continue to do so each time you call it.
If you change the number of a.fav
elements in any way, it would be better to use delegate()
or live()
to bind a click event.
Upvotes: 3