How do you store the value of a checked radio button in local storage using pure Javascript?

Barry Michael Doyle

I'd like to store my checked radio button in local storage so that on a page reload it will have the last checked item check on the reload.

Html:

<h3>Seconds Display:</h3>
<p id="secondsHidingText">Hiding seconds extends battery life</p>
&nbsp&nbsp<input type="radio" name="seconds" value="show" checked="checked">Show&nbsp&nbsp
<input type="radio" name="seconds" value="hide">Hide

This is the javascript I have:

localStorage.setItem('seconds', options.seconds);

(which runs when the save button I have is clicked)

And

document.getElementById('seconds').value = localStorage.getItem("seconds");

(which runs on document being loaded)

I get the following error: Uncaught TypeError: Cannot set property 'value' of null

How do I store and retrieve the checked radio button to and from local storage? And if possible I'm looking for a pure JS way of accomplishing this.

Thanks!

rahul bose

You would have to iterate through the radio buttons and then set the value. Something like this

var radios = document.getElementsByName("seconds"); // list of radio buttons
var val = localStorage.getItem('seconds'); // local storage value
for(var i=0;i<radios.length;i++){
  if(radios[i].value == val){
      radios[i].checked = true; // marking the required radio as checked
  }
}

I used jquery only to set the localstorage value in a convenient way.

Here is a demo

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I check if a radio button is checked using JavaScript?

From Dev

how to get checked radio button value in javascript

From Dev

Store animated GIF image in local Storage using pure JavaScript

From Dev

How do i check if a specific Radio button is checked among the other radio buttons using Javascript?

From Dev

how to store radio button value in javascript variable?

From Dev

How do you reference the checked value in a radio input control for a javascript function?

From Dev

Using Laravel 4, how do I mark a radio button as checked if a session key is a specified value?

From Dev

How do I get the checked radio button value using only React?

From Dev

How to get all radio button value into Json and load using local storage?

From Dev

javascript radio button return previous checked value

From Dev

Javascript: How to get the value from a checked radio button when more than one radio button is present

From Dev

Using the radio button 'checked' value in HTML and PHP

From Dev

How to display checked value for radio button collection

From Dev

How to display the checked radio button value in angularJs

From Java

How do I store data in local storage using Angularjs?

From Dev

How to get the value of the checked radio button and the remaining td's but not include the radio button that has not been checked (in tr)

From Dev

If radio button checked submit value?

From Dev

how to Store the radio button value in another page

From Dev

Issue with Radio button checked in JavaScript

From Dev

get value of checked radio button using d3.js

From Dev

recognize which radio button is checked using enum as value, winforms

From Dev

AngularJS Change Checked Value of Radio Button Using $scope variable

From Dev

How can I send radio button value in PHP using javascript?

From Dev

checked radio button after tr onclick in pure javascript. Performace also matter

From Dev

checked first radio button as default using javascript only

From Dev

checked first radio button as default using javascript only

From Dev

How to reset Spinner value to "Select by something" based on Radio Button checked

From Dev

How to retrieve radio button value from database in checked state in jsp

From Dev

How to send the checked radio button value to Server in angular js

Related Related

  1. 1

    How do I check if a radio button is checked using JavaScript?

  2. 2

    how to get checked radio button value in javascript

  3. 3

    Store animated GIF image in local Storage using pure JavaScript

  4. 4

    How do i check if a specific Radio button is checked among the other radio buttons using Javascript?

  5. 5

    how to store radio button value in javascript variable?

  6. 6

    How do you reference the checked value in a radio input control for a javascript function?

  7. 7

    Using Laravel 4, how do I mark a radio button as checked if a session key is a specified value?

  8. 8

    How do I get the checked radio button value using only React?

  9. 9

    How to get all radio button value into Json and load using local storage?

  10. 10

    javascript radio button return previous checked value

  11. 11

    Javascript: How to get the value from a checked radio button when more than one radio button is present

  12. 12

    Using the radio button 'checked' value in HTML and PHP

  13. 13

    How to display checked value for radio button collection

  14. 14

    How to display the checked radio button value in angularJs

  15. 15

    How do I store data in local storage using Angularjs?

  16. 16

    How to get the value of the checked radio button and the remaining td's but not include the radio button that has not been checked (in tr)

  17. 17

    If radio button checked submit value?

  18. 18

    how to Store the radio button value in another page

  19. 19

    Issue with Radio button checked in JavaScript

  20. 20

    get value of checked radio button using d3.js

  21. 21

    recognize which radio button is checked using enum as value, winforms

  22. 22

    AngularJS Change Checked Value of Radio Button Using $scope variable

  23. 23

    How can I send radio button value in PHP using javascript?

  24. 24

    checked radio button after tr onclick in pure javascript. Performace also matter

  25. 25

    checked first radio button as default using javascript only

  26. 26

    checked first radio button as default using javascript only

  27. 27

    How to reset Spinner value to "Select by something" based on Radio Button checked

  28. 28

    How to retrieve radio button value from database in checked state in jsp

  29. 29

    How to send the checked radio button value to Server in angular js

HotTag

Archive