EasterMedia
EasterMedia

Reputation: 63

Jquery Replace() segment of Dynamic data-url string?

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

Answers (1)

CertainPerformance
CertainPerformance

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

Related Questions