lundi 11 octobre 2021

keep checboxes checked and color of label same after page refresh

I am creating checkboxes dynamically for Wishlist icons. I want to maintain checkbox's checked states in local storage for that I am storing label's for attributes in local storage but i am unable to get it and change wishlist icon's color on page load. and also keep the checkboxes checked. How can I get that checkbox element from ids that I am storing in local storage.

here is my code // php

if(isset($_SESSION['logged-in'])){
            $output .= "<input type='checkbox' id='{$row["post_id"]}' class='fav-icon-checkbox' autocomplete='off'>
                        <label class='fav-icon-label' for='{$row["post_id"]}'><i class='fa fa-heart fav-icon'></i></label>";
        }

//jquery

var checked = [];

  $(document).on('change','.fav-icon-checkbox',function(){
     var target = $(this).next('.fav-icon-label');
   if($(this).is(':checked')) {
    target.css('color','#f7b731');
    checked.push(target.attr("for"));
    localStorage["checked"] = JSON.stringify(checked);
   }
   else{
     target.css('color','#9d9e9e') 
   }

  })

var Getchecked = JSON.parse(localStorage["checked"]);



Aucun commentaire:

Enregistrer un commentaire