Question

Hi i have getelementbyid and i want change it on class but dont work can you help me ? I want change the storageKey and storageData to getelementbyclass if it possile.

Code of Javascript

    function addStorage() {
            var key = document.getElementById('storageKey');
            var data = document.getElementById('storageData');

            //localStorage setItem
            if ("localStorage" in window) {
                localStorage.setItem(key.value, data.value);
                location.reload();
            } else {
                alert("no localStorage in window");
            }

            //sessionStorage setItem
            if ("sessionStorage" in window) {
                sessionStorage.setItem(key.value, data.value);
                location.reload();
            } else {
                alert("no sessionStorage in window");
            }
        }

        function removeStorage() {
            var key = document.getElementById('removeKey');

            //localStorage removeItem
            if ("localStorage" in window) {
                if (localStorage.length > 0) {
                    localStorage.removeItem(key.value);
                    location.reload();
                }
            } else {
                alert("no localStorage in window");
            }

            //sessionStorage removeItem
            if ("sessionStorage" in window) {
                if (sessionStorage.length > 0) {
                    sessionStorage.removeItem(key.value);
                    location.reload();
                }
            } else {
                alert("no sessionStorage in window");
            }
        }

        function clearStorage() {
            //localStorage clear
            if ("localStorage" in window) {
                if (localStorage.length > 0) {
                    localStorage.clear();
                    location.reload();
                }
            } else {
                alert("no localStorage in window");
            }

            //sessionStorage clear
            if ("sessionStorage" in window) {
                if (sessionStorage.length > 0) {
                    sessionStorage.clear();
                    location.reload();
                }
            } else {
                alert("no sessionStorage in window");
            }
        }

        window.onload = function () {
            var localhtml = "";
            var sessionhtml = "";

            //localStorage key and getItem
            for (var i = 0; i < localStorage.length; i++) {
                localhtml += "<li>" + localStorage.key(i) + " : " + localStorage.getItem(localStorage.key(i)) + "</li>";
            }
            document.getElementById('localStorageData').innerHTML = localhtml;

            //sessionStorage key and getItem
            for (var j = 0; j < sessionStorage.length; j++) {
                sessionhtml += "<li>" + sessionStorage.key(j) + " : " + sessionStorage.getItem(sessionStorage.key(j)) + 

"</li>";
            }
            document.getElementById('sessionStorageData').innerHTML = sessionhtml;
        }

And this is html code

    Storage key :
<input type="text" id="storageKey"  style="width: 50px"  >
value :
<input type="text" id="storageData" style="width: 50px" >
Was it helpful?

Solution

I believe you meant to use document.getElementsByClassName instead - try that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top