jQuery Remember toggleClass status in list

Viewed 43

I have a function:

function toggle(event) {
    $(`#${event.target.id}`).toggleClass('fa-power-on fa-power-off');
}

I also have a list of buttons:

<i id="id1" onclick="toggle($event)"></i>
<i id="id14" onclick="toggle($event)"></i>
<i id="id12" onclick="toggle($event)"></i>
<i id="id17" onclick="toggle($event)"></i>
<i id="id18" onclick="toggle($event)"></i>
<i id="id1w" onclick="toggle($event)"></i>

I need the status of the toggleClass icon to be remembered for each.

Either by localStorage or id's and status all in an object and then saved on localstorage or other.

The <i></i> list is dynamic so I cannot hardcode it.

How can I do this?

1 Answers

Consider the following example.

$(function() {
  function toggle(el) {
    $(el).toggleClass("fa-power-on fa-power-off");
  }

  function getPowerStatus(selector) {
    var results = [];
    $(selector).each(function(i, el) {
      var key = $(el).attr("id");
      var val = $(el).hasClass("fa-power-on");
      var item = {};
      item[key] = val;
      results.push(item);
    });
    return results;
  }

  function saveStatus(stats) {
    localStorage.setItem("status", JSON.stringify(stats));
  }

  function getStatus() {
    return JOSN.parse(localstorage.getItem("status"));
  }

  $("i[id^='id']").click(function(e) {
    toggle(this);
    var status = getPowerStatus("i[id^='id']");
    console.log(status);
    saveStatus(status);
  });
});
.fa-power-off {}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css" integrity="sha512-iBBXm8fW90+nuLcSKlbmrPcLa0OT92xO1BIsZ+ywDWZCvqsWgccV3gFoRBv0z+8dLJgyAHIhR35VZc2oM/gI1w==" crossorigin="anonymous" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<i id="id1" class="fas fa-power-off"></i>
<i id="id14" class="fas fa-power-off"></i>
<i id="id12" class="fas fa-power-off"></i>
<i id="id17" class="fas fa-power-off"></i>
<i id="id18" class="fas fa-power-off"></i>
<i id="id1w" class="fas fa-power-off"></i>

This created functions to toggle, gather the status of each, set, and get those status to Local Storage.

Not sure what Icon Set you're using, but Font-Awesome does not have a fa-power-on. Which would mean you'd want to toggle a different class.

$(function() {
  function toggle(el) {
    $(el).toggleClass("on");
  }

  function getPowerStatus(selector) {
    var results = [];
    $(selector).each(function(i, el) {
      var key = $(el).attr("id");
      var val = $(el).hasClass("on");
      var item = {};
      item[key] = val;
      results.push(item);
    });
    return results;
  }

  function saveStatus(stats) {
    localStorage.setItem("status", JSON.stringify(stats));
  }

  function getStatus() {
    return JOSN.parse(localstorage.getItem("status"));
  }

  $("i[id^='id']").click(function(e) {
    toggle(this);
    var status = getPowerStatus("i[id^='id']");
    console.log(status);
    saveStatus(status);
  });
});
.fa-power-off {
  border-radius: 50%;
}

.fa-power-off.on {
  background: black;
  color: white;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css" integrity="sha512-iBBXm8fW90+nuLcSKlbmrPcLa0OT92xO1BIsZ+ywDWZCvqsWgccV3gFoRBv0z+8dLJgyAHIhR35VZc2oM/gI1w==" crossorigin="anonymous" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<i id="id1" class="fas fa-power-off"></i>
<i id="id14" class="fas fa-power-off"></i>
<i id="id12" class="fas fa-power-off"></i>
<i id="id17" class="fas fa-power-off"></i>
<i id="id18" class="fas fa-power-off"></i>
<i id="id1w" class="fas fa-power-off"></i>

Now you can Click them on or off. Each time click event happens, it toggle the class and updates the saved status. You can then get the status when the page loads and re-apply the on class as needed.

Related