“Simpan beberapa radio yang diperiksa di LocalStorage” Kode Jawaban

Simpan beberapa radio yang diperiksa di 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");
      }
    })
  });
}
SAMER SAEID

Simpan beberapa radio yang diperiksa di 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">
SAMER SAEID

Jawaban yang mirip dengan “Simpan beberapa radio yang diperiksa di LocalStorage”

Pertanyaan yang mirip dengan “Simpan beberapa radio yang diperiksa di LocalStorage”

Lebih banyak jawaban terkait untuk “Simpan beberapa radio yang diperiksa di LocalStorage” di JavaScript

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya