Hi I am using jquery to make an ajax request to the database to add and remove favourites from the database. This works fine however I want to replace part of the href in the anchor link so that a user can add/remove again if required without refreshing the page e.g. the link is built as so http://article.local/favourite/delete/uniqueid therefore I need to replace the 'delete' with add and vise versa for the add favourite button. However I can't use the class name otherwise this will apply to all of the classes rather than the one clicked at that time.
$( ".remove-favourite" ).click(function(e) {
e.preventDefault();
var favform = $(this).parent('.fav-form-contents');
$(favform).append("<img src='/images/loading.gif' class='form-loader' class='loading-icon'/>");
$.ajax({
type : "POST",
cache : false,
url : $(this).attr('href'),
data : $(this).serialize(),
success : function(data) {
$('.loading-icon').hide();
$(this).attr('href').replace(/delete/, 'add');
$(this).removeClass('remove-favourite').addClass('add-favourite');
}
})
}); // end click function
However the error message I get back is as follows:
Uncaught TypeError: Cannot read property 'replace' of undefined
This suggests that it has lost the current item, any ideas as to what I am doing wrong??