Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR 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");
      }
    })
  });
}
 
PREVIOUS NEXT
Tagged: #Save #multiple #radios #checked #LocalStorage
ADD COMMENT
Topic
Name
3+5 =