5 Replies - 590 Views - Last Post: 26 July 2014 - 12:25 PM

#1 andynov123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 24-May 11

Help with storing data in localstorage

Posted 25 July 2014 - 10:25 PM

I've been trying to get this to work for a week now with no luck. I added a HTML field to local storage using the updatePricesFunction() I also created a plunker so you can see my issue hands on. http://plnkr.co/edit...VhnLg?p=preview The value for Before Noon doesn't change when you click the blue button "update prices" change the value for before noon and then click the green button "update pricing"
Can anyone help me to get this working?

Is This A Good Question/Topic? 0
  • +

Replies To: Help with storing data in localstorage

#2 andynov123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 24-May 11

Re: Help with storing data in localstorage

Posted 25 July 2014 - 11:34 PM

Ignore my first post.
I got it semi working with localStorage
I have the html document setup like body onload="load()" but the problem is when I try and set the new value for beforenoonprice nothing changes and it stays 6.75 ??
// Javascript Document
var beforenoonprice = 6.75; // CHANGE THE PRICE OF THE BEFORE NOON TICKET
var matineeprice = 9.00; // CHANGE THE PRICE OF THE MATINEE TICKET
var seniorprice = 9.25; // CHANGE THE PRICE OF THE SENIOR TICKET
var militaryprice = 9.25; // CHANGE THE PRICE OF THE MILITARY TICKET
var studentdayprice = 8.00; // CHANGE THE PRICE OF THE STUDENT DAY TICKET
var seniordayprice = 6.75; // CHANGE THE PRICE OF THE SENIOR DAY TICKET
var adultprice = 10.75; // CHANGE THE PRICE OF THE ADULT TICKET
var childprice = 8.00; // CHANGE THE PRICE OF THE CHILD TICKET
var threeDprice = 3.50; // CHANGE THE PRICE OF THE REGULAR 3D PRICE
var imaxPrice = 4.50; // CHANGE THE PRICE OF THE IMAX TICKET
var imax3dPrice = 5.50; // CHANGE THE PRICE OF THE IMAX 3D TICKET
var output = document.getElementById('output');



function updatePricingFunction()
 {
var fieldValue = document.getElementById('beforeNoonNPSlot').value;
localStorage.setItem('text', fieldValue);

 }
 
 function load(){
	 var storedValue = localStorage.getItem('text');
	 if(storedValue){
		beforenoonprice = storedValue;
		 
		 }
	 
	 }

 	
function displayTotal(){


//PRICE CHANGE EDIT ENDS HERE


var beforenoon = Number(document.getElementById('beforeNoon').value) || 0;
var beforenooncost = beforenoon * beforenoonprice;
var matinee = Number(document.getElementById('matinee').value) || 0;
var matineecost = matinee * matineeprice;
var senior = Number(document.getElementById('seniorTicket').value) || 0;
var seniorcost = senior * seniorprice;
var Military = Number(document.getElementById('military').value) || 0;
var military = Military * militaryprice;
var StudentDay = Number(document.getElementById('studentDayTicket').value) || 0;
var studentdaycost = StudentDay * studentdayprice;
var seniorDay = Number(document.getElementById('seniorDayTicket').value) || 0;
var seniordaycost = seniorDay * seniordayprice;
var Adult = Number(document.getElementById('adultTicket').value) || 0;
var adultcost = Adult * adultprice
var child = Number(document.getElementById('childTicket').value) || 0;
var childcost = child * childprice;
var threeD = Number(document.getElementById('threed').value) || 0;
var threeDcost = threeD * threeDprice;
var Imax = Number(document.getElementById('imax').value) || 0;
var imaxCost = Imax * imaxPrice;
var Imax3d = Number(document.getElementById('imax3d').value) || 0;

var imax3dCost = Imax3d * imax3dPrice;

var total = childcost+adultcost+military+seniorcost+studentdaycost+seniordaycost+threeDcost+imaxCost+imax3dCost+beforenooncost+matineecost;

document.getElementById('calculate').innerHTML = total.toFixed(2);





}






<input type="text" id="beforeNoon" class="input-small" placeholder="Before Noon"><br>


Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3480
  • View blog
  • Posts: 11,875
  • Joined: 12-December 12

Re: Help with storing data in localstorage

Posted 26 July 2014 - 05:07 AM

Are there any errors in your browser's console?

var storedValue = localStorage.getItem('text');

The beforenoonprice will only change if storedValue has a value (other than 0/empty). Print out the value of storedValue:
console.log(storedValue);
// or display it in one of your inputs.

If not then a value isn't being saved successfully in localStorage.

You can normally check localStorage in the browser, as you can with cookies. I'll guess that some browser's also require you to allow 'local file access' before you can work (locally) with localStorage. Added: Actually, it works for me in GC without this setting, so this may not be an issue.

This post has been edited by andrewsw: 26 July 2014 - 05:09 AM

Was This Post Helpful? 0
  • +
  • -

#4 andynov123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 24-May 11

Re: Help with storing data in localstorage

Posted 26 July 2014 - 11:58 AM

Actually I got it working last night. But I do have one more question.
I have a variable that is outputted to my html document, but I'm having problems adding two decimal places to the value. Normally I would use parseFloat(value).toFixed(2) but I can't seem to figure out how to use this method or any other with .append.
How can I make this appear like "$4.00" instead of appearing like "$4"?
	$('#beforeNoonPrice').append("$" + storedValue);	);


Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3480
  • View blog
  • Posts: 11,875
  • Joined: 12-December 12

Re: Help with storing data in localstorage

Posted 26 July 2014 - 12:19 PM

$('#beforeNoonPrice').append("$" + storedValue + ".00");

or if storedValue is a number:
$('#beforeNoonPrice').append("$" + storedValue.toFixed(2));

otherwise
$('#beforeNoonPrice').append("$" + (storedValue * 1).toFixed(2));

This post has been edited by andrewsw: 26 July 2014 - 12:23 PM

Was This Post Helpful? 0
  • +
  • -

#6 andynov123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 24-May 11

Re: Help with storing data in localstorage

Posted 26 July 2014 - 12:25 PM

Thanks! That did the trick.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1