jQuery - store items in temporary storage using hidden input

Store items in temporary storage using hidden input

<?php
<input type="hidden" value="" name="package_collection" id="package_collection">
<
script>
function
add_package_to_collection(selected_package_id, selected_package){
        var
package_collection_storage = $('#package_collection').val();
        var
package_collection = {};
       
        if(
package_collection_storage.length > 0){
            try {
               
package_collection = JSON.parse(package_collection_storage);
              } catch (
ex) {
               
console.error(ex);
              }
        }else{
           
        }
       
       
package_collection[selected_package_id] = selected_package;
       
        $(
'#package_collection').val(JSON.stringify(package_collection));
       
console.log(JSON.stringify(package_collection));
    }
</script>
?>
Knowledge keywords: