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
nicholas maddren
12,793 Points“onclick” Store Value of Element Into Variable (Web Storage API)
Hello I am currently working on a family project and seem to be having a problem with JavaScript & Web Storage API.
So I am basically trying to place the value of an a tag into a variable that can be used on all pages (Web Storage API).
I have found a way to do this with static HTML using ID's however my page is dynamic as it is an eCommerce listings page so I couldn't possible add a specific ID to every a tag and give each a variable.
So I have this a tag :
<a href="/book-testdrive" class="addtocart" title="
<?php echo $this->__('Book Test Drive') ?>">
<i class="glyphicon glyphicon-road"></i>
<span> <?php echo $this->__('Book Test Drive') ?></span>
</a>
When this is clicked I would like the value (Example: value="TS56POD") to be stored within the Web Storage API. I will then use Javascript to display the value on other pages. If a user clicks another 'Book Test Drive' a tag on the page I'd like the variable value to replaced.
So basically I do NOT NEED each tag to have a unique variable as I'd like the value to be replaced for each a tag the user clicks with the addtocart class.
Can somebody possibly show me an example of how an onclick event could store the value of the element within a variable?
Here is an example of my listings page for better understanding: http://drivencarsales.co.uk/used-cars.html
Thanks, Nick