Reputation: 63
Trying to create an Ajax add to wishlist button which alters the data-url of a given link depending on whether or not it exists in the array.
Think i'm on the right line but can't get the .replace() to actually implement.
How am I using replace() incorrectly here?
var wishlistBtn = $('.add-to-wish');
var wishlistIds = [];
$.each(wishlistBtn, function(){
var curId = $(this).data('id');
var curURL = $(this).data('url');
var cur = $(this);
if($.inArray(curId, wishlistIds) > -1){
cur.addClass('in-wishlist');
curURL.replace('Add', 'Delete');
} else {
cur.removeClass('in-wishlist');
curURL.replace('Delete', 'Add');
}
});
The data-url is generated dynamically with twig.php
Upvotes: 0
Views: 105
Reputation: 370839
.replace
only gives you the replaced string - it performs no side-effects. If you want a side effect, you have to do it explicitly - assign the replaced string to the url
property:
if($.inArray(curId, wishlistIds) > -1){
cur.addClass('in-wishlist');
$(this).data('url', curURL.replace('Add', 'Delete'));
} else {
cur.removeClass('in-wishlist');
$(this).data('url', curURL.replace('Delete', 'Add'));
}
Upvotes: 1