What steps can I take to ensure that the content remains intact even after the page is

Hey there, I hope you're having a great start to the New Year! Recently, I've been working on creating a calculator using HTML, CSS, and JavaScript. One thing that's been puzzling me is how to make sure that the content in the input field doesn't disappear when the page is refreshed. I'm reaching out for some assistance on this issue. Just to note, I haven't implemented much logic into the calculator yet.

Here's the code snippet:


var text = document.getElementById("field");

function type(word) {
  text.value += word;
}
* {
  margin: 0;
  padding: 0;
  color: white;
}

body {
  background-color: black;
  display: flex;
  justify-content: center;
  transform: translateY(50%);
}

input {
  background-color: transparent;
  border: 1px solid white;
  width: 250px;
  height: 70px;
  margin-bottom: 20px;
  font-size: xx-large;
  text-align: right;
}

td {
  width: 60px;
  height: 60px;
  text-align: center;
  border-radius: 50%;
  border: 1px solid white;
  cursor: pointer;
  font-weight: bold;
  font-size: larger;
}

td:hover {
  color: black;
  background-color: white;
}
<html>

<head>
  <title>Calculator</title>
</head>

<body>
  <main>
    <input type="text" id="field" maxlength="11">
    <table>
      <tr>
        <td onclick="type('%')">%</td>
        <td onclick="text.value=''">C</td>
        <td onclick="type(')')">)</td>
        <td onclick="type('÷')">÷</td>
      </tr>
      <tr>
        <td onclick="type('7')">7</td>
        <td onclick="type('8')">8</td>
        <td onclick="type('9')">9</td>
        <td onclick="type('×')">×</td>
      </tr>
      <tr>
        <td onclick="type('4')">4</td>
        <td onclick="type('5')">5</td>
        <td onclick="type('6')">6</td>
        <td onclick="type('-')">-</td>
      </tr>
      <tr>
        <td onclick="type('1')">1</td>
        <td onclick="type('2')">2</td>
        <td onclick="type('3')">3</td>
        <td onclick="type('+')">+</td>
      </tr>

      <tr>
        <td onclick="type('(-')">+/-</td>
        <td onclick="type('0')">0</td>
        <td onclick="type('.')">.</td>
        <td>=</td>
      </tr>
    </table>
  </main>
</body>

</html>

Answer №1

If you find that the input is always being cleared when you refresh the page, one convenient solution is to utilize the localStorage API to retain the input value.

You can refer to this helpful post for more information.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Angular Validation displays ng-valid when the form is actually invalid

I am currently working on a wedding RSVP form https://i.stack.imgur.com/Ct8Ux.png My objective is to hide the DONE submit button and only display it when the form is considered valid. <form method="POST" action="http://l.bheng.com:8888/wedding" acce ...

Transform an array of values into a new array with a set range

Looking for a solution in JavaScript! I currently have an array of values: var values = [3452,1234,200,783,77] I'm trying to map these values to a new array where they fall within the range of 10 to 90. var new_values = [12,48,67,78,90] Does anyo ...

What is the process of creating a model instance in a Nodejs controller?

Trying to work with the model object in Node using the sequelize module. It looks something like this: File structure: models index.js user.js controllers userController.js routes route.js ========================== models/users.js //created us ...

Automated form with built-in calculations

Whenever a product is selected from the dropdown menu, the price value should be automatically filled in the input field with ID #price. Then, the user can enter the quantity in the input field with ID #quantity, and the total sum of price multiplied by qu ...

How to Vertically Align a div in the Center using Bootstrap 5

Is there a way to horizontally center a div using bootstrap 5? I want the div to be positioned in the middle, between the content above it and the end of the screen. I attempted the following: <div>some content</div> <div class="d-flex ...

Having trouble with tabs in jQuery?

I'm having trouble setting up tabs in a reservation form with 3 tabs that include text boxes for user input. I can't seem to get it working properly and I'm not sure where I've gone wrong. Could it be due to the placement of the content ...

Retrieve, process HTML table information using Ajax from an external server or website domain

In order to demonstrate: :the html table data source = "example.com/html_page_source" (tableid="source") :an xml file created from the specified table data = "pretendco.com/xml_built_from_html_table I am seeking guidance on how to build an xml file from ...

Navigating URLs to Single Page Application Routing in Vue.js

I'm new to using vue-router and I'm curious if there's a way to manage redirection in Vue or if there are alternative methods in a node.js environment. For instance, when someone tries to access my site by typing the URL example.com/contac ...

What is the best way to implement a scroll-into-view feature for a newly added list item in Vue.js?

I am currently utilizing vueJS to create a task viewing application. My goal is to make the div containing the list focus on the newly added list item immediately after adding a new task. Here's the HTML code from my template for the task list: < ...

Tally the number of items using the jQuery AJAX method and the

Extracting information from an XML DB looks something like this: <record> <eventname></eventname> <company></company> <city></city> <state></state> </record> The data is then parsed usi ...

Is there a way to showcase the generated results on a graph after the user presses the submit button?

I've been working with to generate a bar chart. Currently, when the user clicks 'submit', the input numbers are shown in a graph on http://jsfiddle.net/jx9sJ/5/. Now, I want to make some changes. I am attempting to send the input numbers t ...

The problematic max-width with srcset attribute

Here's the HTML markup I'm working with: <picture> <source srcset="img/home-page/placeholders/placeholder_2560.jpg" media="(max-width: 2560px)"> <source srcset="img/home-page/placeholders/placeh ...

What is the best way to loop through ng-repeat with key-value pairs in order to display each

I need to loop through and show the data stored in "detailsController". <div ng-controller="detailsController"> <div ng-repeat="data in details" id="{{data.Id}}"> {{data.Name}} </div> </div> ...

elevate the div with a floating effect

My goal is to create a chat feature for users that will only be visible for a limited time period. I was thinking of using PHP and timestamp to achieve this functionality, but I also want to make the chat visually disappear by having the message div float ...

Unraveling JavaScript using regular expressions in Python

I need to parse some JavaScript text using Python. Within the JS code, there is a variable like this: this.products = ko.observableArray([#here is some json, #here is some json]) The observableArray can hold a single JSON object like: observableArray({&a ...

ever-evolving background-image with dynamic CSS styling

Being new to both PHP and Javascript, please excuse any mistakes in my explanation. I have information stored in a PHP array that I bring to my index page using the function below (located in a separate file called articles.php that is included in my index ...

Can you explain the distinction between the two methods of coding in jQuery?

1, $.each(a,f); 2,$("a").each(f); I understand the purpose of the last line, where it selects all <a> elements in the document and then calls the each() method to invoke function f for each selected element. However, I am unsure about the meani ...

In the process of making a request with axios in an express server

Struggling with a simple call to an API using Axios with Express, and I can't figure out why it's always pending in the browser. Here is my route: var express = require("express"); var router = express.Router(); const controller = require("../. ...

Sending an array of functions to the onClick event of a button

Having difficulty with TypeScript/JavaScript Currently working with an array of functions like this private listeners: ((name: string) => void)[] = []; Successfully adding functions to the array within another function. Now looking to trigger those ...

Guide to creating nested collapsing rows in AngularJS

My attempt to implement expand and collapse functionality in AngularJS for a section is not yielding the desired result. To demonstrate, I created a simple demo of collapsible/expandable sections in AngularJS which works fine. You can view it here. The ex ...