2

I have dynamically generated buttons with url using data attributes

<button class="btn-Add" type="button" data-add-url="/AddProductToCart?mobileNo=__param__&amp;
productId=2051&amp;quantity=1">Add to Cart</button>

I have the following code:

$(document).ready(function () {
    var mobileNo = 0;
    $(".btn-Add").click(function () {
        mobileNo = $(this).parent('.pro-Group').find('input[type=text]').val();
        var postURL = $(this).data('add-url');
        postURL.replace('__param__', mobileNo);
        alert(postURL); // i can still see __param__ , its not replaced
    });
});

Why query string value is not replaced after using replace method ?

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
Shaiju T
  • 6,201
  • 20
  • 104
  • 196

1 Answers1

3

Javascript Strings are immutable. So once their value got assigned they can't changes their values.

So you have to reassign them with new changes or have to receive them in new variables. Generally we reassign as there is no need of new variable to be introduced unless you need old value .

  postURL = postURL.replace('__param__', mobileNo);
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307