Dale
Dale

Reputation: 62

How do I use session variables with jQuery?

I have a button on one page another button on a different page. This second button has the text in it "0 items selected". When the first button is clicked I want to increase the number in the second button by one.

Seeing it is transferring data over different pages I didn't manage to do it the standard way.

Please do not suggest PHP, I am unable to use it.

var yetVisited = localStorage[0];

if ($('.#CallToActionCustomise').click()){
	localStorage++;
}
$('.#CallToActionCustomise').click(function(){
	$(".Main_MenuButtonReview").append(localStorage);
});
<button class="Main_MenuButtonReview">0 items selected <i class="fa fa-caret-down"></i></button>

Upvotes: 0

Views: 46

Answers (2)

kangtaku
kangtaku

Reputation: 162

In addition to the above answer, I think you need to init some tag when it loaded.

Put this code on another page.

$(document).ready(function() {
    if (typeof localStorage.yetVisited === 'undefined') {
        localStorage.setItem('yetVisited', 0);
    }
    yetVisited = localStorage.yetVisited;
    $('.Main_MenuButtonReview').text(yetVisited + ' items selected');
});

$(document).ready(function() {
 // this area means after your page(document) ready completely.
});

Upvotes: 0

Bricky
Bricky

Reputation: 2745

You need to access localStorage as if it were an Object, not an array.

Also, you can only store String values, so to get a number to perform math on, you will need to use parseInt.

localStorage.setItem('yetVisited', 0);

$(".Main_MenuButtonReview").text(localStorage.yetVisited + ' items selected');

$('.CallToActionCustomise').click(function()){
    localStorage.yetVisited = parseInt(localStorage.yetVisited) + 1;
    $(".Main_MenuButtonReview").text(localStorage.yetVisited + ' items selected');
}

Here's an example.

Upvotes: 1

Related Questions