The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

> Displaying name entered via form and saved via Sessionstorage
Max Eisner
post Nov 7 2015, 10:37 AM
Post #1


Unregistered









Hello.
As the title describes, I wanted to display a value (here a name) that was entered by a form and saved to SessionStorage in an html document. Since I'm totally new to js, I don't get it to work... Now I ask you guys for some help.
I tried the following, which I found as examples/templates on several tutorial sites and adjusted for my case, but it won't work.

CODE

<!DOCTYPE html>

<head>
    <meta charset="UTF-8">
    <title>Web/LocalStorage</title>
</head>

<body>
    <form id="Login/Name">
        <div class="field">
            <label for="name">Name</label>
            <input type="text" name="name" id="name">
        </div>
        <div class="field">
            <input type="submit" value="Senden">
        </div>
    </form>
    <script>
        window.onload = function () {
            document.getElementById('Login/Name').addEventListener('submit', function () {
                var name = document.getElementById('name').value;
                sessionStorage.setItem('name', name);
            });
        }
    </script>
    <script>
        window.onload = function () {
            var name = sessionStorage.getItem('name');

            if (name != "undefined" || name != "null") {
                document.getElementById('NameZeigen').innerHTML = "Hello " + name + "!";
            } else
                document.getElementById('NameZeigen').innerHTML = "Hello!";
        }
        }
    </script>
</body>


Thanks for your help! As you do any changes, it would be nice if you add comments to it so I can better understand what you did so I will learn to do for myself faster wink.gif
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Posts in this topic


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 19th May 2024 - 08:59 AM