The values are not being displayed in the local storage checkbox

I attempted to transfer checkbox values to another HTML page using local storage, however they were not appearing on the other page

Here is my page1 HTML code snippet:

<input class="checkbox" type="checkbox" id="option1" name="car1" value="BMW">
            <label for="vehicle1">BMW</label><br>
            <input class="checkbox" type="checkbox" id="option2" name="car2" value="Ferrari">
            <label for="vehicle2">Ferrari</label><br>
            <input checkbox="checkbox" type="checkbox" id="option3" name="car3" value="Ford">
            <label for="vehicle3">Ford</label><br>
            <input class="checkbox" type="checkbox" id="option4" name="car4" value="Audi">
            <label for="vehicle4">Audi</label><br>
            <input class="checkbox" type="checkbox" id="option5" name="car5" value="Cadillac">
            <label for="vehicle5">Cadillac</label><br>

This is the Javascript code I used:

for (var i = 0; i < inputs.length; i++) {
        if (inputs[i].type == "checkbox" && inputs[i].checked) {
            count2++;
            localStorage.setItem("checkvalue", inputs[i].value);

        }
    }

<script>
// This is the page2 script
            <span id="result"></span>,Welcome
            document.getElementById("result").innerHTML = localStorage.getItem("checkvalue");
        </script>

Answer №1

It was pointed out by @SMAKSS that you are constantly replacing your localstorage during each loop.

Instead of

for (var i = 0; i < inputs.length; i++) {
  if (inputs[i].type == "checkbox" && inputs[i].checked) {
    count2++;
    localStorage.setItem("checkvalue", inputs[i].value);
  }
}

You might want to consider

localStorage.setItem("results", JSON.stringify(inputs));

Then, retrieve it with

JSON.parse(localStorage.getItem("results"));

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

Node.js can be used to easily set the values of HTML elements

After successfully setting up a node.js connection to my mysql database and being able to retrieve and input data using connection.query(), I now want to display the database information on my HTML elements. Is there an equivalent of getElementById for t ...

Using JQuery and PHP to make an Ajax request and handle JSON response

I'm attempting to complete a straightforward exercise: The task involves entering two numbers in separate inputs, clicking a button, and seeing the result appear in a third input. sum.html: <html> <head> <title>Sum</tit ...

Design a PHP tool for generating custom CSS frameworks

After attempting to create a CSS frame generator similar to this one, I stumbled upon a solution that works well with one exception. PHP Code: <?php function print_css($parentTag, $prefix, &$dup_checker) { if ($parentTag->nodeName == &apos ...

Executing a Shortcode Using a Button in Visual Composer for Wordpress

It should be easy to do this. I've got a great plugin with a modal newsletter signup form that offers various launch options, including manual launching with the following codes. https://i.stack.imgur.com/IGbsp.png My theme utilizes Visual Composer. ...

The android application experiences crashing issues when utilizing the position or zIndex style properties within a react-native environment

In my code, I am attempting to display a semi-transparent black screen over my page in order to show a message or prompt in the center. I have tried using zIndex or elevation with position:'fixed' or position:'obsolet', and it works per ...

PHP code to send an HTML template via email:

When attempting to send HTML content in an email using my PHP mail code, I encountered an issue where the HTML template was not being sent along with the message. $to = $email;; $subject = 'New Order Detail&apo ...

Unveiling the Mystery: Uncovering the Selected Item in Ionic Checkboxes

I am trying to implement a feature in Ionic checkboxes where I can get the selected item when a user checks one or more checkboxes. Specifically, I want to identify which books the user has selected. Below are snippets of my code: <ion-item ng ...

Guide on implementing CSS modules in a WordPress plugin built with React

Within one of my tsx files, I included this line: import styles from "../../styles/buyTicket.module.css"; This resulted in the following error message: ERROR in /Applications/MAMP/htdocs/wp-content/plugins/tikex/tikexModule/components/BuyTicket/ ...

Shuffle contents of a Div randomly

I'm currently in the process of developing a new website and I need to randomly move the news feed. To achieve this, I have created an array containing the list of news items. The array is then shuffled and placed within the news feed section. Althou ...

What is the best way to use shortcodes to display custom fields for post objects, specifically products, within WordPress posts

I'm in the process of displaying 'featured products' within my blog posts. These specific products need to be chosen using custom post objects on the backend for each individual post. I've outlined what I believe the PHP code should lo ...

What is the best way to include attributes in an HTML element?

I've been researching how to dynamically add attributes to an HTML tag using jQuery. Consider the following initial HTML code: <input type="text" name="j_username" id="j_username" autocorrect="off" autocapitalize="off" style="background-image: lin ...

Stop Stripe checkout if all other fields are left empty

I am working on a simple "booking" function using stripe and I encountered this issue. Below is my form code: <form id="formid" action="/checkout" method="POST"> <input type="text" name="kurtuma" id="zaza"> <script src="//check ...

Reorganize the JSON data to match the specified format

{ "EUR": { "Description": "", "Bid": "1.1222", "Region": "", "Bid1": "1.1283", "CurrencyCode": "EUR", "FeedSource": "1", "Ask": "1.1226", "ProviderName": "TEST 1", "Low": "1.1195", ...

How can I use CSS to transform a "+" symbol to an "x"?

When I created an accordion using HTML, JS, and CSS, I encountered a problem. The "+" sign in the accordion does not rotate correctly as it should. Instead of rotating from the middle of the "+" sign, it rotates from the top. This is my HTML code: <di ...

Having a parameter that contains the characters '&' and '&' can potentially disrupt an AJAX call

Even though there is a similar question here: Parameter with '&' breaking $.ajax request, the solutions provided do not apply to my specific issue. This is because both the question and answers involve jQuery, which I am not familiar with. I ...

Tips on preventing right-click actions in jqGrid

While utilizing onSelectRow in a jqGrid, I have noticed that it functions properly when clicking with the left mouse button. However, when right-clicking, it still triggers the function. My aim is for the right-click to perform its usual action (such as di ...

Creating an XPath expression for selecting multiple siblings of a div tag

Currently, I am working on writing an XPath expression for a specific section of code: <div class="line info"> <div class="unit labelInfo TextMdB">First</div> <div class="unit lastUnit"> <div clas ...

The announcement will not be made as a result of the utilization of a private name

I've been working on transitioning a project to TypeScript, and while most of the setup is done, I'm struggling with the final steps. My goal is to use this TypeScript project in a regular JavaScript project, so I understand that I need to genera ...

Creating Styles in Material-UI using makeStyles: Tips for Styling by Element Name

I am currently facing a challenge of adding a rule for all <p> tags within the current component. Despite searching through various sources such as material-ui documentation and Stack Overflow, I have been unable to find any information on how to add ...

What is the method for inserting form control values into a QueryString within HTML code?

Struggling with passing HTML form control values into a QueryString for page redirection. While I can easily input static values into a QueryString and retrieve them using PHP's GET method, I am encountering difficulties when it comes to dynamic valu ...