The structure of the news is about as follows.

<div id="1"> ..... <a href="">кнопка</a> </div> <div id="2"> ..... <a href="">кнопка</a> </div> <div id="3"> ..... <a href="">кнопка</a> </div> 

How to add the class views to the div but only to the one in which the button is located and write it in the cookie here with this plugin https://github.com/carhartl/jquery-cookie

Here is something that has been made, but only the last clicked link `var cookieName = 'level'; var cookieOptions = {expires: 7, path: '/'};

 $("#" + $.cookie(cookieName)).addClass("views"); $(".play").click(function(e){ e.preventDefault(); $.cookie(cookieName, $(this).attr("id"), cookieOptions); $("#" + $.cookie(cookieName)).addClass("views"); });` 

Links such <div class="shortplaypanel"><a id="Player{news-id}" href="">кнопка</a></div> and so on, only {news-id} changes

How to make all links with different id memorized, and not just the last clicked?

  • Or simply if there are a bunch of links on the page <a id="1" href="">кнопка</a> and <a id="2" href="">кнопка</a> by clicking to change the style of this link . So that after reloading the page, it remains for the user in the same state - steep

1 answer 1

 $("a").on("click", function(e){ e.preventDefault(); $(this).closest("div").addClass("views"); $.cookie('views', $(this).closest("div").attr("id")); }); 

And do not forget that id should begin with a letter.

Reading cookies after reloading the page and adding a class:

 var id = $.cookie('views'); $("#" + id).addClass("views"); 
  • The class is added, but not remembered. The div with the button changed to such a <div id="box{news-id}"> <a href="">кнопка</a> </div> - steep
  • In the cookies there should be an id of the layer with the button pressed, check again - Vasily
  • I have {news-id} in each block different - steep
  • Reload page, this class disappears - steep
  • And this class is added to other buttons, to all buttons on the site - steep