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

What are the steps to run a webpack project without relying on webpack-dev-server?

I've been working on hosting my project on GitHub pages by creating a /doc file and placing all my HTML, CSS, and JS there. If you're interested, you can check out my project here: https://github.com/mattfrancis888/the_movie_db The only way I&a ...

Ensuring that the text input is perfectly lined up with the submit button

Is there a way to solve this discrepancy? Solution: https://jsfiddle.net/6q352ysx/59/ The elements are currently the same height, but not aligned properly. https://i.stack.imgur.com/ajVyJ.png Which alignment option should I use? Option 1: vertical-ali ...

Finding the next div by its ID using jQuery

Currently, the jQuery code provided only allows navigation from the Sport section to the Entertainment section. Is there a way to create a script that allows navigation between any div sections with just one piece of code? <div id="topBar"> &l ...

The Catcomplete widget malfunctioned after the update of jQuery UI from version 1.8 to 1.12

I've encountered an issue with my code after updating to jQuery UI 1.12 var currentCategory = ""; $.widget("custom.catcomplete", $.ui.autocomplete, { _renderMenu: function (ul, items) { var self = this; $.each( ...

Retrieve a specific child element by clicking on a custom control on the map

I have a container element with three child spans. Using JQuery, I can determine which child was clicked when the container is clicked by utilizing the following function: $('.unitContainer').on('click','span',function(){ ...

React - the use of nested objects in combination with useState is causing alterations to the initial

After implementing radio buttons to filter data, I noticed that when filtering nested objects, the originalData is being mutated. Consequently, selecting All again does not revert back to the original data. Can anyone explain why both filteredData and orig ...

Implementing CSS keyframes when a specified PHP condition is satisfied

I am looking to implement an opening animation on my website that should only play when a user visits the site for the first time. I want to avoid displaying the animation every time the page is reloaded, so it should only run if a cookie indicating the us ...

I'm encountering a typescript error as I migrate a Paho MQTT function from Angular 1 to Angular 2 - what could be causing this issue?

When connecting to my MQTT broker, I am working on several tasks. In my Ionic 2 and Angular 2 application, I have created an MQTT provider. Here is the provider code: import { Component } from '@angular/core'; import { NavController, ViewControl ...

Toggle Button Control

In my PHP file, I have a submit button for the form coded like this: <input type="submit" name="submit" value="submit" disabled="true" /> Next, I initiate an asynchronous request using a request object, document.getElementById("username").onblur= ...

What is the more effective choice for Automation Testing: Static or Dynamic dropdown menu options?

Currently facing an issue with dropdown selections while conducting automated testing. Selenium's Select(); method has been reliable when running tests in multiple cycles and choosing different options each time. The challenge arises when the dropdow ...

Connect to content on the current page

I am facing an issue where the linked heading of a section on the same page is getting lost under the fixed navigation bar when scrolling down. This problem seems to only occur on desktop view as it works fine on mobile preview. Here is the code I am curre ...

Creating a Duplicate of the Parent Element and its Child Elements using jQuery

Here is a code snippet I have that adds a new paragraph when a button is clicked. Currently, the script clones the "sub" div and appends it to the "main" div. However, the issue is that it only copies the content of the "inner" div within the "sub" div ins ...

In JavaScript, the addition and subtraction buttons may become disabled after nested lists are used

Recently, I embarked on a project to enhance the functionality of restaurant table items and implement value-saving features. Initially, everything worked smoothly with one item list. However, upon introducing a second list and attempting to manipulate it ...

Unlocking the Sound: Using a Custom Button to Activate Audio on HTML Video in React

While working on a project, I encountered a small issue: I have a video tag in my code: <video muted autoPlay loop src={video}> I simply want to add a single custom button/control to toggle between muting and unmuting the video. I'm thinking of ...

Leveraging AngularJS ngBind with a JavaScript object

Within the given string, integrating a javascript object and embedding it into an ngBinding does not result in proper evaluation. I have a string where I want to incorporate a specific part of a javascript object and am transitioning to Angular for its use ...

Providing UI field attributes within server JSON data

Suppose I have numerous form fields that need to be displayed on the user interface. Additionally, in a standard Modify workflow, let's assume that these fields will have various attributes like value, mandatory, editable, disabled, label, regex (for ...

Paper.js: Is there a way to prevent canvas height and width from changing when the window is resized?

My canvas with paperjs is set up to resize dynamically when the window is resized. I appreciate any help in advance. HTML <canvas id="myCanvas" height="800" width="1000"></canvas> JS var Initialize = function () { var canvas = document ...

Tips for capturing audio in flac codec format using JS/AJAX

Is there a way to record audio in flac codec instead of opus codec? I attempted setting the codec to flac like this: let blob = new Blob(audioChunks,{type: 'audio/ogg; codecs=flac' }); I also tried this: var options = { audioBitsPerSecond : ...

Can you explain the purpose of CSS classes such as my-2, my-lg-0, and mr-sm-2 in Bootstrap 4?

I have searched through various questions and the Bootstrap documentation but haven't been able to determine the purpose of these classes on elements. Any assistance would be greatly appreciated! My goal is to adjust my search bar's size to 1/4th ...

Is there a way to restrict form choices depending on the previous entry, while also keeping all fields, including file selections, filled out upon submission?

Currently, I have a form set up as follows: <form name="estimate" action="live_preview.php" method="post"enctype="multipart/form-data"> <label for="file">Upload File 1:</label> <input type="file" name="file" id="file"/> ...