Reputation: 18103
$(document).ready(function() {
$('a#fav').bind('click', function() {
addFav(<?php echo $showUP["uID"]; ?>);
});
});
function addFav(id){
$.ajax({
url: "misc/favAdd.php",
data: { id: id},
success: function(){
$('a#fav')
.addClass('active')
.attr('title','[-] Remove as favorite')
.unbind('click')
.bind('click', removeFav(id))
;
jGrowlTheme('wallPop', 'mono', '[+] Favorit', 'Du har nu lagt till denna profil som favorit', 'images/addFavorit_hover2.png', 1000);
}
});
}
function removeFav(id){
$.ajax({
url: "misc/favRemove.php",
data: { id: id },
success: function(){
$('a#fav')
.removeClass('active')
.attr('title','[+] Add as favorite')
.unbind('click')
.bind('click', addFav(id))
;
jGrowlTheme('wallPop', 'mono', '[-] Favorit', 'Du har nu tagit bort denna profil som favorit', 'images/addFavorit_hover2.png', 1000);
}
});
}
I have this issue, that when i click on a#fav then it addClass active, and removes it and add and remove and send a bunch of ajax itself.. it goes crazy. And i do not want that, i want to have it so when you click a#fav it adds class (send ajax call) and then when you click it again it sends ajax call and removes the class. And it goes on like that..
What have i done wrong?
Upvotes: 1
Views: 1410
Reputation: 887797
By writing .bind('click', removeFav(id))
, you are calling removeFav
immediately and passing its return value to the bind
function.
Instead, you need to pass a function that calls removeFav
, like this:
.bind('click', function() { removeFav(id); })
(And similarly for addFav
later)
Upvotes: 3