SANTHIYA
SANTHIYA

Reputation: 15

Local Storage of all Events

   function pushButton(buttonValue) {
    if (buttonValue == 'C') {
        document.getElementById('screen').value = '0';
    }
    else {//this is where most changes occured
        var x= document.getElementById('screen').value 
        x =parseInt(x)+ parseInt(buttonValue);
        document.getElementById('screen').value=x;
        localStorage.setItem('answer', x);
    }
}

    function calculate(equation) {
        var answer = eval(equation);
        document.getElementById('screen').value = answer;
    }

HTML:

    <table class="calc" cellpadding=2> 
        <td><input type="button" class="calc" id="screen" value="0" ></td>
        <tr>
    </table>
    <table class="calc" cellpadding=2> 
        <td><button type="button" onclick="pushButton(1)" value="Call2Functions">1</td>
        <td><button type="button" onclick="pushButton(2)" value="Call2Functions">2</td>
        <td><button type="button" onclick="pushButton(3)" value="Call2Functions">3</td>
        <td></td>
    </tr>
    <div>

even after trying this code i a unable store value locally. Pls can anyone help me? Still i am finding solution for this issue..after adding localStorage also i am unable to store values locally Kindly help me i have tried a lot...

Upvotes: 0

Views: 45

Answers (1)

Moishe Lipsker
Moishe Lipsker

Reputation: 3034

To Store items in the localStorage, you will need to store the value paired with a key using localStorage.setItem:

var key = 'yourKey'
,   resultToStore = 'test';
localStorage.setItem(key, resultToStore);

To retrieve the value, just call localStorage.getItem and pass in the key:

var returnedResult = localStorage.getItem(key);

In your function pushButton, you would want to store the value:

function pushButton(buttonValue) {
    if (buttonValue == 'C') {
        document.getElementById('screen').value = '0';
    }
    else {//this is where most changes occured
        var x= document.getElementById('screen').value 
        x =parseInt(x)+ parseInt(buttonValue);
        document.getElementById('screen').value=x;
        localStorage.setItem('answer', x);
    }
}

Now whenever page loads, you need to see if there is a value in the localStorage and get that value if it's there:

window.onload = function() {
    var oldAnswer = localStorage.getItem('answer');
    if(oldAnswer) {
        document.getElementById('screen').value = oldAnswer;
    }
}

Upvotes: 1

Related Questions