Wednesday, May 16, 2012

How do I refresh the page on click if an element has exact text?

I have a link that says 'Add to Favorites'. By default when you click this link and you are logged in, you get diverted to a confirmation page to say your favorite has been added. If you are not logged in it takes you to a login screen.



If you have already added that item to your favorites the text says 'Remove from Favorites' with an updated link to include '?A=Remove'. If you click it, you are redirected to a page confirming the favorite removal.



Now what I want to do is this:



If you're not logged in get an alert message.



If you are logged in - Refresh the page (Ideally I'd like to change 'Add to favorites' to 'Favorite added successfully' instead of refreshing the page - but I don't know how).



If you have already added to the favorites - Refresh the page (Ideally I'd like to change 'Remove from Favorites' to 'Favorite removed successfully' instead of refreshing the page - but I don't know how).



The {module_isloggedin} tag is generated by the system I am using. It generates a '1' if logged in & '0' if not logged in.



So far all that happens is the error message - Perhaps because my :contains isn't working?



The whole code seems much too long and sloppy. I'm still learning :)



This is my jQuery so far, I haven't put in a page refresh for the 'remove from favorites' yet.



if(jQuery('.add-favourites').size() === 0){ return; }

jQuery('.add-favourites a').live('click', function(e){

e.preventDefault();
var loggedin = "{module_isloggedin}";

if (loggedin == 1) {
if (jQuery('.box-content ul li:contains("Add to Favorites")').length > 0) {
document.location.reload(true);
}else{
if(jQuery(this).attr('href').indexOf("A=Remove") != -1){
jQuery.ajax({
url: jQuery(this).attr('href'),
cache: false
});
}else{
jQuery.ajax({
url: jQuery(this).attr('href')+"&A=Remove",
cache: false
});
}
}
}else{
alert('You must be logged in to add favorites.');
}
});




No comments:

Post a Comment