Input cache when updating page

Asked

Viewed 32 times

-1

You can keep the information typed in the Input by updating the page only using front end?

input {
border-color: blue;
padding:5px;
font-weight:bold;
}
<input type="text" value="Manter este texto com ID" id="cache">
<input type="text" value="Não manter sem ID">

1 answer

2


Yes! You can use localStorage

<!DOCTYPE html>
<html>
<head>
    <title>Teste</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <style type="text/css">
        input {
            border-color: blue;
            padding:5px;
            font-weight:bold;
        }
    </style>
</head>
<body>


    <input type="text" id="cache">
    <input type="text">

</body>

<script type="text/javascript">
    var cache = localStorage.getItem("cache");
    $('#cache').val(cache);
    $('#cache').keyup(function() {
      localStorage.setItem("cache", this.value);
    });
</script>
</html>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.