What is the best way to use local storage for increasing poll counts?

I am currently working on a feature that will increment and store poll tallies in local storage if selected. While I have successfully implemented the poll count functionality, I am facing challenges when it comes to individually incrementing each of the three choices and saving them to local storage.

let pollCount = 0;

function getPollCount() {
  return "Total Votes: " + pollCount;
}
function incrementPollCount() {
  let nominee1 = document.getElementById("ges");
  let nominee2 = document.getElementById("glp");
  let nominee3 = document.getElementById("prs");

  if (nominee1.checked) {
    pollCount++;
  } else if (nominee2.checked) {
    pollCount++;
  } else if (nominee3.checked) {
    pollCount++;
  }
}
<main>
  <h2>Please Select Nominee!</h2>
  <form  name="nominee" onsubmit="voteSubmit()">
    <label>
      <input type="radio" 
         name="nominee" id="ges" value="Gibson ES-335">
    </label> Gibson ES-335
    <div id="orderTotal1">
      <p id="p1"></p>
      <script>document.write(getPollCount())</script>
    </div>
    <label>
      <input type="radio" 
             name="nominee" id="glp" value="Gibson Les Paul">
    </label>
      Gibson Les Paul
      <div id="orderTotal2">
        <p id="p2"></p>
        <script>document.write(getPollCount())</script>
      </div>
    <label>
      <input type="radio" 
              name="nominee" id="prs" value="Paul Reed Smith">
   </label> 
     Paul Reed Smith
     <div id="orderTotal3">
       <p id = "p3"></p>
       <script>document.write(getPollCount())</script>
     </div>
     <input type="submit" 
         name="select" value="Select" onsubmit="incrementPollCount()">
  </form>
</main>

Answer №1

To efficiently manage poll counts, you can save three distinct values in the local storage as demonstrated below:

function getPollCount() {
    return "Total Votes: " + getIndividualPollCount('nom1') + getIndividualPollCount('nom2') + getIndividualPollCount('nom3');
}

function updateCount(entry) {
    localStorage.setItem(entry, (Number(localStorage.getItem(entry)) || 0) + 1)
}

function getIndividualPollCount(entry) {
    Number(localStorage.getItem(entry) || 0)
}

function incrementPollCount() {
    let nominee1 = document.getElementById("ges");
    let nominee2 = document.getElementById("glp");
    let nominee3 = document.getElementById("prs");

    if (nominee1.checked) {
        updateCount('nom1')
    } else if (nominee2.checked) {
        updateCount('nom2')
    } else if (nominee3.checked) {
        updateCount('nom3')
    }
}

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

Exploring AngularJS: Effiecient Looping and Styling

Being a beginner in AngularJS, please excuse me if this question seems silly. I want to display my data in a grid format (using Ionic) where each row has separate columns like this: <div class="row"> <div class="col-33">Item 1</div> ...

Leverage the Axios package to make requests within a for loop

I'm new to JavaScript and currently working on a project using Vue.js, Axios, and the API available at . The goal of this project is to retrieve NBA player statistics for a homework assignment. I could use some assistance in addressing certain issues. ...

Accessing a peaceful API and displaying the outcome on a webpage using Node.js

I am currently working on a project that involves fetching data from a RESTful API and displaying the results on an HTML webpage using Node.js. While my code is running smoothly, I would like to ensure that the RESTful request is made every time the webp ...

Full scale intricate grid arrangement

Is there a way to make this header 'full width' so that the image appears on the left side of the screen while keeping the content in the grid? https://i.sstatic.net/U6pft.png I was considering creating a new container. .container--full { max- ...

Display an HTML checkbox with intricate design features

I have a simple question - how can I print a green checkbox with a white check mark (Ctrl + P) without it turning black and white? This solution currently works with Bootstrap 4.0.0, but needs to be compatible with bootstrap 3.3.7. I've managed to cr ...

Activating the first item in a custom menu in WordPress

I'm trying to make the first custom menu item active by adding an "active" class. Can anyone help me achieve this? Here is the HTML code: <nav class="secondmenu"> <div class="menu-captain-crew-container"> <ul id="menu-capta ...

Passport JS receiving negative request

I'm currently experiencing an issue with the passport req.login function. When a user logs in using their email and password, instead of redirecting to /productos2 as intended, it routes to a bad request page. I am utilizing ejs and mongoose for this ...

Transferring this object to inner functionality within JavaScript

Within my code, I have a function defined inside another function: function Test(message) { this.message = message; } Test.prototype.print = function() { console.log(this.message); } Test.prototype.print.polite = function() { console.log(thi ...

The unexpected occurence of the Onbeforeunload exception

I am attempting to create an exception for onbeforeunload and display a warning about potential data loss whenever the quantity is not zero: Here is what I have tried so far: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www. ...

Issues with CSS hover event causing animation to fail to trigger

I have a container that contains an image holder and a text holder. I am able to set it so that when hovered over, the image scales and the same effect applies to the text. However, when attempting to set up the image holder hover to scale the image and ...

Enhance Your Button with CSS3 Animation

While experimenting with CSS3 animations, I encountered a challenge when trying to apply an animation to a button upon clicking. The desired effect was for the button to zoom in, spin, and then zoom out, giving the appearance of flying off the screen. Surp ...

The bond between TypeORM and express

I am working on establishing Many-to-One and One-to-Many relationships using TypeORM and MySQL with Express. The database consists of two tables: post and user. Each user can have multiple posts, while each post belongs to only one user. I want to utilize ...

Properly ending a connection with the `mysql` package in Node.js

Here's a funny question that's been on my mind. I recently integrated an API in Node.js using the mysql package for the project. This is how I implemented it: // Establishing Connection const connection = mysql.createConnection({ host: host, ...

Encountering a theme issue in the makeStyles function within Material-UI version 5

While working on some React components, I created a styles.js file for each of them. I am following a YouTube tutorial that uses material-ui version 4, so I decided to upgrade to V5. Code snippet for the component (Form): import React from 'react&apo ...

What is the best way to conduct end-to-end testing on an Amazon Linux AMI?

Running end to end tests using Protractor has been smooth in Chrome and Firefox on Ubuntu. However, I encountered issues with PhantomJS as it could not locate the elements. My Angular version is v1.2.15. My aim is to test on an Amazon Linux AMI, so either ...

Eloquent JavaScript Chapter 5 Poser: Can you solve the exercise question using the array.some method

This particular challenge really had me scratching my head for a while. After struggling with it, I finally caved and resorted to looking up the solution online. The hint provided was quite cryptic, and dealing with "De Morgan's Laws" in JavaScript us ...

Avoid an excessive number of XHR/AJAX requests when using the Facebook embedded iframe

I am facing an issue with a Bootstrap Carousel that contains multiple social embeds from Facebook, all of which have videos. The problem is evident on this simple jsfiddle due to the Facebook embed. If you visit this page: https://jsfiddle.net/1L95vqn4/, ...

Setting up a dropdown list with a Django variable following an AJAX request

I am facing an issue with implementing a dropdown that triggers an AJAX call when a new value is selected. In my setup, the AJAX script calls a Django view which returns a list of values. The problem arises when I try to populate a second dropdown with the ...

What is the best way to create a transparent effect over a solid color using CSS in the following situation?

I have been using the spinner on this website "". Specifically, I am referring to the third one in the first row. When I apply it to my system, a solid color appears in the center of the circle. However, I want to make it transparent, but when I attempt ...

Is there a way to showcase the contents of the angular "tags" object seamlessly?

Is there a way to show the content of the "tags" object using angular? I attempted to achieve it using {{gallery.tags.tag}} but unfortunately, it did not work import {IPhoto} from "./iphoto"; export interface IGallery { galleryId: string; title: ...