Dennis
Dennis

Reputation: 11

Store onclick event in browser with jQuery Cookie

I'm working on a newsfeed of posts that on click filters out the the type of content listed.

<div class="option all"    onclick="Stories('all',0);"></div>
<div class="option photos" onclick="Stories('photos',0);"></div>
<div class="option videos" onclick="Stories('videos',0);"></div>      
<div class="option music"  onclick="Stories('music',0);"></div>

This is the code i use to customize css style for each button and save it.

$('.option').eq($.cookie('active')).addClass('clicked');
$(".option").click(function (e) {
    $.cookie('active', $('.option').index(this));
    e.preventDefault();
    $(this).toggleClass("clicked");
    $(".option").not(this).removeClass("clicked");
});

How can i store the "onclick" event with jquery.cookie.js? On browser reload the default "all" shows but I would want the last "onclick" option of posts to show instead and not just the css style so I need a way to save the "onclick"

Upvotes: 0

Views: 1654

Answers (1)

ianaya89
ianaya89

Reputation: 4233

I recommend use localStorage instead a cookie:

You can handle localStorage easy without any library and is supported in all modern browsers (IE8 +):

localStorage.setItem('lastClicked', 'Your value here'); //save
localStorage.getItem('lastClicked'); //retrieve

Check out the documentation.

Here is the browser support.

[Update] - Integrating code with click event

$(document).ready(function(){ 
  $('.clicked').click(function(){
    localStorage.setItem('lastClicked', this); //replace this for whatever you want to store
  });
});

Upvotes: 2

Related Questions