0

I have been working on the script below for a while now and I am stuck. I have tried to see what is wrong and I think it is something small.

<form name="f1">Gold:
    <input type="text" value='0' name="t1" id="a2" disabled>
    <br>
    <input type="button" value="Click me!" id="a1" onclick="clickAdd">
    <br>Jewels:
    <input type="text" value="0" name="g1" id="a3" disabled>
    <br>Cost: 50 Gold
    <input type="button" value="5 jewels" id="a4" onclick="click5()">
    <br>Cost: 100 Gold
    <input type="button" value="10 jewels" id="a5" onclick="click10()">
    <br>
</form>
<script>
    function clickAdd() {
        //Detects if local stoage click count exists
    if (localStorage.getItem("clickcount") == null || NaN) {
            localStorage.setItem(clickcount, 0);
    document.getElementById("a2").value = localStorage.getItem('clickcount');
            //Sets the clickcount to zero
        } 
    else {
            //If it already exists.    
            localStorage.setItem("clickcount",localStorage.clickcount + 1);
            document.getElementById("a2").value = localStorage.getItem('clickcount');
        }
    }
</script>

Can someone please fix it or tell me what is wrong.

T J
  • 42,762
  • 13
  • 83
  • 138
CodeMaster
  • 15
  • 3
  • Compare the code for setting the item in the `if` and the code for setting the item in the `else`. Also your null check is a bit off. You haven't told us your exact problem though, what does work? What doesn't work? – SlashmanX Mar 28 '14 at 16:46
  • It wont create the localstorage itself and thats the reasons the rest wont work. – CodeMaster Mar 28 '14 at 16:51

1 Answers1

1

First of all add the parens to invoke the function as follows

<input type="button" value="Click me!" id="a1" onclick="clickAdd()">

Change "null" to null.

then for updating -

localStorage.setItem("clickcount", parseInt(localStorage.getItem('clickcount')) + 1);;

and for setting the value -

document.getElementById("a2").value = localStorage.getItem('clickcount');

You should use getItem() and setItem() methods to access local storage. Check this SO question for more info

JSfiddle Demo

Community
  • 1
  • 1
T J
  • 42,762
  • 13
  • 83
  • 138