Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Understanding Javascript Cookies/Sessions

Hi guys so on my website I have buttons that have values. When a button is clicked I want to have the value that has been given to that button to be placed into a variable within my JS file.

So for example I have this button:

<a href="/book-testdrive" value="NX56PQV" class="test-drive-btn" title="Book Test Drive"><i class="glyphicon glyphicon-road"></i><span>&nbsp;Book Test Drive</span></a>

As you can see it has a value inside, when this A tag is pressed I want that value to be placed onto my global JS file within a variable, however have it within a cookie so that when the page is refreshed the variable isn't.

I then have a page that has an input field with an ID, I want to change the value of the input field with the one of the variable that is in the cookie. So basically taking the variable that we have just set by pressing the A tag and loading it as a value when the page loads with the input field that has a specific ID.

Are there any tutorials that might be able to help me with this or can anyone show me an example? I have searched cookie tutorials however I still don't know where to start with this.

Thanks, Nick

1 Answer

You can bind an onclick eventhandler on your button which stores the value into the localstorage of your browser. I would also give the button an id to make it unique. For example your button would be:

<a id="btn" href="/book-testdrive" value="NX56PQV" class="test-drive-btn" title="Book Test Drive"><i class="glyphicon glyphicon-road"></i><span>&nbsp;Book Test Drive</span></a>

Using Javascript you can target your button and store it in a variable, for example:

var btn = document.getElementById('btn');

After that add an event listener to the button which takes the value of the value-attribute and stores it in the local storage of the browser when clicked:

btn.addEventListener('click', function(){
  var btnValue = this.getAttribute('value'); //store the value of the "value" attribute in a local variable
  if(!localStorage.getItem('myValue')) { //checking if myValue already exists
    localStorage.setItem('myValue', btnValue); //store it by giving it a name and assigning the value
  }
});

After the value has been stored you can use it on every page using localStorage.getItem('myValue');

You can find your stored value if you open the console of your browser (try Chrome) and open the "Resources" tab and after that "Local Storage" on the left.

You can read more about it here:

https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API/Using_the_Web_Storage_API