Tell me how to implement the following mechanism There is a link by clicking on which we get the text values ​​of the span from the parent unit with the name class and price_per_unit . And then the cookie check whether the value matches the text value of the span with the name class, if there is, then delete this value from the cookie. Cook has the following structure

 basket1: "["Текст","860"]" basket2 "["Книга","160"]" 

To work with the cookie using trace. library https://github.com/js-cookie/js-cookie

 $(document).on("click",".del", function(e){ e.preventDefault(); var removeProduct = $(this).closest(".block").find("span.name").text(); var removePrice = $(this).closest(".goods").find("span.price_per_unit").text(); $(this).closest(".goods").hide(); obj = document.cookie; for (var prop in obj) { if(obj == removeProduct) { obj = null; } } }); 
  • What is the problem? Is your code not working? - Yuri
  • deletion of a specific value does not occur - ChromeChrome
  • But where is the use of the library in the question? - Grundy
  • Maybe this: $.cookie("name", null, { path: '/' }) or so $.removeCookie('name', { path: '/' }); ? Not? - Yuri
  • @Yuri, no, because in this case there is no $ .cookie_ like _ $. RemoveCookie - Grundy

2 answers 2

 var cookies = Cookies.get(); // получаем все куки (с помощью библиотеки js-cookie) var value2remove = '["' + removeProduct + '","' + removePrice + '"]'; // собираем искомое значение в формате ["Продукт","цена"] for (var cookie_name in cookies) { // перебираем все полученные куки if(cookies[cookie_name] == value2remove) // если это то самое значение - ... Cookies.remove(cookie_name); // удаляем куку по имени // если так и не удалит - раскомментить эту строчку и сверить значения, // т.к. я не уверен, что в полях "name" и "price_per_unit" лежат // данные в корректном формате, и возможно понадобится над ними поколдовать //console.log(cookie_name, cookies[cookie_name], value2remove); } 
  • your option doesn't work either - Grundy
  • well then console.log in a loop, display the prop and remuvprodukt, see if they are true - AlexandrX
  • and, in general, prop, it will be an index, not a value .. well, the console.log will show everything - AlexandrX
  • Well, the question is: how to remove? your code - removes absolutely nothing from cookies - Grundy
  • This is not my code, I just changed the variable))) and what do the js-cookie docks say about this? - AlexandrX

Use jQuery Cookie Library

Connection:

 <script src="/path/to/jquery.cookie.js"></script> 

Install the cookie:

 $.cookie('name', 'value', { expires: 7 }); 

Delete cookie:

 $.removeCookie('name'); // => true 

And about the basket, it is better to make an array of goods and write it in cookies:

 var items = [], _cart = {}; _cart.add = function (item){ $.each(items, function (index, cart_item){ if (JSON.stringify(cart_item) == JSON.stringify(item)){ items[index].qty++; _cart.renewCookieCart(); return 'Item quantity has been improved'; } _cart.renewCookieCart(); items.push(item); return 'Item has been added'; }); } _cart.remove = function (index){ return items.splice(index, i); } _cart.renewCookieCart = function (){ return $.cookie('basket', JSON.stringify(items)); } _cart.getCart = function (){ return JSON.parse($.cookie('basket')); } 
  • The author in question indicated that he already uses one of the libraries for working with cookies. Why advise him another? - Grundy
  • @Grundy I have provided a complete answer with an alternative basket. Is there something bad here? - Roman Kozin