0

i have an input which displays data in local storage underneath. At the moment it currently keeps adding whatever has been inputted and displays with an '' in between. This is fine however all I need is for it to Store and display ONE input and display it. If the user wants to change the input, there will be a button which clears local storage.

Can anybody help with the function for this.

<script>

function storeText(inputID) {

//check to see if the localStorage item already exists
var userInput = localStorage.userInfo;

//set it up for new data to be appended
if(userInput!=null) userInput+=" ";
else userInput="";

//add the new data
userInput += document.getElementById(inputID).value;

//set the localStorage field with the updated data
localStorage.userInfo = userInput;

//write it to the page
document.getElementById("result").innerHTML = localStorage.userInfo;

if (userInput > 1){
alert("Please Clear to add a new goal");
return false;

}

</script>

This is my Input and display

<input id="userText" type="text"/>
    <input type="button" value="store" onclick="storeText('userText')"/>
    <div id="result">           
    Result here
</div>
Gopal Joshi
  • 2,350
  • 22
  • 49
user2336624
  • 47
  • 1
  • 1
  • 12

1 Answers1

0

localStorage.clear(); - this will clear local storage entirely

localStorage.removeItem('userInput'); - this removes a specific item - you can then just re-add it.

Just apply this to your buttons onClick event

GrahamTheDev
  • 22,724
  • 2
  • 32
  • 64
  • this is good thankyou!!but is there away of preventing the user form adding more than one? cheers for the wuick reply – user2336624 Jan 04 '14 at 13:15
  • Can you elaborate - do you want to have multiple inputs or just one input? Also do you want the user to just to be able to store one value - if they add another value it deletes the old? – GrahamTheDev Jan 04 '14 at 17:20
  • i apologise, what you suggested works better than what i was attempting! i used your method, clear local storage onclick and created a show function which displays onclick also. – user2336624 Jan 04 '14 at 18:02
  • for some reason, when i use the RemoveItem, instead of removing the displayed input, it removes the button input itself. Have you any idea what the problem is? – user2336624 Jan 05 '14 at 01:20
  • post your new code - specifically the part that has .removeItem. I am guessing you missed localStorage off the beginning of the phrase and have named the button userInput but can't be sure. This is a new question so post it as new question and then just add a quick comment to this thread so I know to look out for it (with the new question name) – GrahamTheDev Jan 05 '14 at 06:36
  • Hi Graham, really appreciate your help, this is my new question. http://stackoverflow.com/questions/20932612/how-to-delete-parts-of-local-storage-with-removeitem – user2336624 Jan 05 '14 at 10:44