Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Save multiple radios checked on LocalStorage

function saveFav() {
  let checked = Array.from(document.querySelectorAll("input[type=radio]:checked")).map(e => e.id);
  localStorage.setItem('checked', JSON.stringify(checked));
}

function setFav() {
  const getChecked = JSON.parse(localStorage.getItem("checked"));
  let radios = [...document.querySelectorAll("input[type=radio]")];
  radios.forEach(e => {
    e.removeAttribute("checked")
    getChecked.forEach(id => {
      if (e.id === id) {
        e.setAttribute("checked", "checked");
      }
    })
  });
}
Comment

Save multiple radios checked on LocalStorage

<div><b>Favorite sport :</b>
  <input type="radio" id="basketball" name="sport" value="basketball" checked="checked">
  <label for="basketball">basketball</label>
  <input type="radio" id="football" name="sport" value="football">
  <label for="football">football</label>
  <input type="radio" id="handball" name="sport" value="handball">
  <label for="handball">handball</label><br>
</div>
<div><b>Favorite fruit :</b>
  <input type="radio" id="banana" name="fruit" value="banana" checked="checked">
  <label for="banana">banana</label>
  <input type="radio" id="apple" name="fruit" value="apple">
  <label for="apple">apple</label>
  <input type="radio" id="pear" name="fruit" value="pear">
  <label for="pear">pear</label>
  <input type="radio" id="raspberry" name="fruit" value="raspberry">
  <label for="raspberry">raspberry</label><br>
</div>
<div><b>Favorite ... :</b> other radios ...<br><br>
</div>
<input onclick="saveFav()" type="button" value="Save favorite">
<input onclick="setFav()" type="button" value="Load favorite">
Comment

PREVIOUS NEXT
Code Example
Javascript :: supertest npm send headers node js 
Javascript :: Browser globals 
Javascript :: python regex consecutive characters 
Javascript :: gradient of a function 
Javascript :: track call recording in facebook using elements 
Javascript :: restrict file input with react uploady 
Javascript :: string split into three non empty combination js 
Javascript :: request submit form 
Javascript :: mongodb create index json 
Javascript :: get oinput value clojurescript 
Javascript :: store api key in environment variable ngular 
Javascript :: json array form to list object java 
Javascript :: in nav link if I click on the same active link, page has to refresh in react js 
Javascript :: angular 13 deploy on tomcat 9 
Javascript :: Class Which Can Create An Instance Of The Same Type 
Javascript :: add attribute to element in jquery 
Javascript :: wait for element to be loaded 
Javascript :: sol.common.MapTable elo 
Javascript :: js get first elements of array 
Javascript :: Create A Class That Returns A Promise In Constructor 
Javascript :: python code to javascript converter 
Javascript :: prisma graphql n+1 problem solution 
Javascript :: check for overlapping time javascript 
Javascript :: var maxNum = function(arr) {}; 
Javascript :: backbone view initialize 
Javascript :: simple JSX example 
Javascript :: react get variable from child component 
Javascript :: react native raw bottom sheet 
Javascript :: how to generate random ip address in javascript 
Javascript :: dictionnary js 
ADD CONTENT
Topic
Content
Source link
Name
2+2 =