Home > database >  transfer captured data from a function with variables to a single Object called 'user' and
transfer captured data from a function with variables to a single Object called 'user' and

Time:01-10

<div id="body-container">
    <form id="user" action="login.html">
        <input type="text" id="firstname" placeholder="Enter your First Name">
        <input type="text" id="lastname" placeholder="Enter your Last Name">
        <input type="number" id="age" placeholder="Enter your Age">
            <select id="gender"" name="gender">
                <option value="Male">Male</option>
                <option value="Female">Female</option>
            </select>
        <input type="text" id="username" placeholder="Enter your Username">
        <input type="password" id="password" placeholder="Enter your Password">
        <br> <br>
        <input type="submit" value="Submit" onclick="register()">
    </form> 
</div>

<footer id="footer">
    <p>Created on November 2019, by <a href = "http://Jon_Doe.com>John Doe</a></p>
</footer>

<script>
     function register(){
         var firstname = document.getElementById("firstname").value
        var lastname = document.getElementById("lastname").value
        var age = document.getElementById("age").value
        var gender = document.getElementById("gender").value
    }
</script>

I have to transfer all the captured data from the function register() and save it in a object called "user" and then save that data to sessionStorage.

CodePudding user response:

You can create an object with your datas :

function register(){
    var firstname = document.getElementById("firstname").value;
    var lastname = document.getElementById("lastname").value;
    var age = document.getElementById("age").value;
    var gender = document.getElementById("gender").value;

    var user = {
        firstname: firstname,
        lastname: lastname,
        age: age,
        gender: gender,
    };
}

And store it with sessionStorage.setItem("user", JSON.stringify(user))

  • Related