0

I am creating a web app which tracks people's weight. I have one page where an array is stored in local storage and displayed to the user.

The page im currently working on now, inputs the users target weight and displays it to them.

I originally had this working with a localStorage.clear() however i noticed that this also wiped my array on the other page. I am trying to use the localStorage.RemoveItem, however it just seems to remove the input button and not the data.

Can anybody help me with this?

This is my script.

UPDATE*

function storeText(inputID) {

//Clears old target weight
//localStorage.clear(); 

//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;




}

 function UpdateWeight(inputID){

    localStorage.userInfo = document.getElementById(inputID).value;

    document.getElementById("result").innerHTML = localStorage.userInfo;

}


</script>

input

<input size="10" id="userText" type="text"/>            
        <input type="button" class="greyButton" value="submit" onclick= "UpdateWeight(inputID)"/>
        <div id="result">           
        Result here
        </div>
user2336624
  • 47
  • 1
  • 1
  • 12

1 Answers1

0

rename the function

remove(); 

to

removeStorage();

remember to update the onClick as well.

remove(); 

is running on the button not calling your function!

A much more sucinct function would be:

function UpdateWeight(inputID){

    localStorage.userInfo = document.getElementById(inputID).value;

    document.getElementById("result").innerHTML = localStorage.userInfo;

}

From what you have shown this does what you want - replaces original item with new item on button click?

GrahamTheDev
  • 22,724
  • 2
  • 32
  • 64