How in the code below to record the presence of values ​​in the input'ah sessionStorage (localStorage)? Now only the last value is saved. View code on JSFiddle

<script> $('#floor, #entrance, #apartment, #intercom').on('blur', function(e) { if ($(this).val()) { $(this).next('.custom-placeholder').toggleClass('completed', true); sessionStorage.setItem("PlaceholderCompleted", $(this).attr("id")); } else { $(this).next('.custom-placeholder').removeClass('completed'); } }); var PlaceholderCompletedId = sessionStorage.getItem("PlaceholderCompleted"); if (PlaceholderCompletedId) { $("#" + PlaceholderCompletedId).siblings(".custom-placeholder").toggleClass('completed', true); console.log(PlaceholderCompletedId); } </script> <style>.completed {color: blue; font-weight:bold;}</style> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div> <div> <input id="apartment" type="text"> <div class="custom-placeholder">Кв./офис</div> </div> <div> <input id="floor" type="text"> <div class="custom-placeholder">Этаж</div> </div> <div> <input id="entrance" type="text"> <div class="custom-placeholder">Подъезд</div> </div> <div> <input id="intercom" type="text"> <div class="custom-placeholder">Код домофона</div> </div> </div> 

    1 answer 1

     $('#floor, #entrance, #apartment, #intercom').on('blur', function(e) { $(this).siblings('.custom-placeholder').toggleClass('completed', $(this).val() != ""); stored[$(this).attr("id")] = $(this).val() != ""; sessionStorage.setItem("CompletedItems", stored); }); var stored = sessionStorage.getItem("CompletedItems"); stored = stored? JSON.parse(stored) : {}; for (var key in stored) { $("#" + key).siblings(".custom-placeholder").toggleClass('completed', stored[key]); console.log(key, stored[key]); } 
    • Thanks, but prints "SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data" - Oleg Nechaev
    • one
      @ OlegNechaev Change the name under which the data is stored. The fact that there is already there — written by the old code — is not JSON. - Igor
    • The result was this: jsfiddle.net/mv80p7rh/44 - Oleg Nechaev