const btnSuchen = document.getElementById('search');
const btnLoeschenKey = document.getElementById('delete_key');
const btnAllesLoeschen = document.getElementById('delete_all');


auslesen();


function auslesen() {

    var count = window.localStorage.length;
    var displayData = document.getElementById('data');
    displayData.innerHTML = "";
    if (typeof(Storage) !== "undefined") {
        for (let i=0; i<count; i++) {
            var keyname = window.localStorage.key(i);
            var data = window.localStorage.getItem(keyname);
            displayData.innerHTML += keyname+": "+data+"<br>";
            
        }
    } else {
        displayData.innerHTML = "Entschuldigung, Ihr Browser unterstützt nicht Web Storage...";
    }
    
}

function finden() {

    var keyname = document.getElementById('inputKey').value;
    var givenKey = document.getElementById('givenKey');
    var notice = document.getElementById('notice');
    givenKey.innerHTML = "";
    notice.innerHTML = "";
    var data = window.localStorage.getItem(keyname);
    givenKey.innerHTML = keyname +":";
    notice.innerHTML = data; 
    
}


function loeschen() {
    var key = document.getElementById('key').value;
	window.localStorage.removeItem(key);
}

function allesLoeschen() {
	window.localStorage.clear();
}

btnSuchen.addEventListener('click',  e => {
    finden()});

btnLoeschenKey.addEventListener('click',  e => {
    loeschen()});

btnAllesLoeschen.addEventListener('click',  e => {
    allesLoeschen()});

