Hello. I want to replace the value of the 'data-quantity' attribute in the link. How to do it? through val, html, text does not work ..

var number = 3; $('a').attr('data-quantity').val(number); 
 <a href="#" data-quantity="1">Купить</a> 

    3 answers 3

     var number = 3; $('a').data('quantity', number); 

    Read more here: https://api.jquery.com/data/

    • And don't forget about selectors (classes or what have you, id?)! The previous answers will change the date-attribute of all elements with the specified date-attribute, no matter how punse it sounds)) !!! - Sergey V.

    It is possible so:

     var number = 3; $('a').attr('data-quantity', number); 
    • the only thing that is bad is js changing, but the initial html remains - if this parameter was not reset when rebooting ... - Vasya
    • Change the parameter on the server so that when it restarts, it takes the desired value. 2nd option: store the data in localStorage, from where take it to js when loading / reloading the page to the browser. - Visman

    like this: $('a').data('quantity',number);