Replace one parameter in data attribute after an ajax call in jquery

Viewed 33

How can I replace one parameter &a in a string with Jquery? I have a button to add or remove users from favourite list with an ajax call. It is working now, but after adding a user to favourite (in ajax.php parameter &a= addtofav) I want to make possible without reloading a page to make a new ajax call to remove user (in ajax.php parameter &a=deletefromfav). How can I change data attr in span after a successful ajax call? Example below. Thanks ahead!

$(document).ready(function(){
$('body').on('click', 'span.favu_add_btn', function(){
var target = $(this);
var ajaxurl = $(this).data("url");
//ajaxurl is ?r=favoriteusers&uid=296&a=addtofav
$.ajax({
url: ajaxurl,
method: "GET",
    }).done(function (h) {
    d = $.parseJSON(h);
    if (d.success === true) {
    target.removeClass('btn-green');
    target.addClass('btn-leaf');
    target.text(OVERALL_LANG['favoriteusers_addedto']);
    //here I want to change data-url attr to ?r=favoriteusers&uid=296&a=deletefromfav
    } else {
    }
    }).fail(function (h) {
    });
});
});  
<span data-url="?r=favoriteusers&amp;uid=296&amp;a=addtofav" class="ajax favu_add_btn btn btn-sm btn-info strait_right" style="padding: 3px 13px !important; border-right:none;">Add to favourite users</span>

0 Answers
Related