0

The list that gets appended to the div, how do I store them so that next time open my browser the appended elements remain as the previous state.

$('#try').prepend('<li class="list-group-item danger">' + $('#dat').html() + '</li>'); This is what I want to append.

 //This is the empty div

    <div id="dat" class="alert alert-danger">
    
    </div>
  
    /*below is my javascript code to append elements(ignore other functions)
    this code basically appends the defects to the empty div*/
    
    var queue = new Array();
    
    function greet(greeting) {
        console.log(greeting);
        $('#dat').text(greeting);
    }
    
    function getRandom(arr) {
        var rand = Math.random();
        return arr[Math.floor(rand * arr.length)];
    }
    
    var greetings = [
        "Hello 1",
        "hello 2",
        "hello 3",
        "Hello 4",
        "hello 5",
        "hello 6",
        "hello 7"
    ];
    
    /*---------------------------*/
    
    
    $(document).ready(function() {
        ion.sound({
            sounds: [
    
                { name: "glass" }
            ],
    
            path: "Scripts/ui control/ring/",
            preload: false,
            volume: 10.0
        });
        setInterval(function() {
            ion.sound.play("glass");
            greet(getRandom(greetings));
    
            $(".alert").fadeIn("fast").fadeOut(5000, function addMessage(msg) {
                queue.push(msg);
                $('#try').prepend('<li class="list-group-item danger">' + $('#dat').html() + '</li>');
    
                if ($('.list-group-item').length > 9) {
                    //var msgToRemove = queue.shift();
                    //msgToRemove.remove();
                    $('.list-group-item:last').remove();
                }
            });
        }, 8000);
    
    });
  
Amir
  • 1,328
  • 2
  • 13
  • 27
ashwin1014
  • 351
  • 1
  • 5
  • 18
  • What about cookie? You need something like this: https://stackoverflow.com/a/4225071/8137468 – guest Jun 13 '17 at 09:50

1 Answers1

0

You can store whatever you want to in a localStorage variable, then use it later :

localStorage.stored = ... ; //Whatever you want to store

...

if(typeof localStorage.stored != 'undefined'){
    $('mySelector').append(localStorage.stored);
}
Zenoo
  • 12,670
  • 4
  • 45
  • 69
  • You'll have to tweak some minor things when accessing from different browsers, but you can use `localStorage ` on multiple browsers. – Zenoo Jun 13 '17 at 11:48