Skip to content Skip to sidebar Skip to footer

Save To Localstorage

i'm curious if its possible to save the text input a user types in the field to localStorage , so in this html example , i'd want to save the innner html from the div#match_player_

Solution 1:

I recommend you to use a javascript function instead:

<scripttype="text/javascript">functionupdateMatchPlayerId() {
    var matchPlayerId = document.querySelector('#contract_year').value;
    document.querySelector('#match_player_id').innerHTML =  matchPlayerId;
    if (typeofStorage !== "undefined") {
        localStorage.setItem("matchPlayerId", matchPlayerId);
    }
}
</script>

HTML:

<input type="text"id="contract_year" placeholder="Contract" autocomplete="off">

<input value="Submit" onclick="updateMatchPlayerId()"type="button">

<div id="match_player_id"></div>

Solution 2:

Firstly, you should really be using unobtrusive event handlers to hook to events instead of the outdated on* event attributes. As you've tagged jQuery in the question this can be done incredibly simply.

From there you can just use localStorage.setItem() to save the value you require, like this:

$('button').click(function(e) {
  e.preventDefault();

  var contractYear = $('#contract_year').val();
  $('#match_player_id').html(contractYear);

  localStorage.setItem(contractYear);
});
<inputtype="text"id="contract_year"placeholder="Contract"autocomplete="off"><buttontype="button">Submit</button><divid="match_player_id"></div>

Working example

Post a Comment for "Save To Localstorage"