Arham Awan
Arham Awan

Reputation: 634

Form data is not storing in local storage

<div>
    <center>
        <form method="post" onsubmit="submit();">
            <label for="fname">First Name:</label>&emsp;
            <input type="text" id="fname" />
            <br/>
            <label for="lname">Last Name:</label>&emsp;
            <input type="text" id="lname" />
            <br/>
            <label for="uname">User Name:</label>&emsp;
            <input type="text" id="uname" />
            <br/>
            <label for="pass">Password:</label>&emsp;&ensp;&nbsp;
            <input type="text" id="pass" />
            <br/>
            <label for="dob">Date of Birth:</label>&emsp;&emsp;&nbsp;
            <input type="date" id="dob" />
            <br/>
            <label>Age:</label>&emsp;&emsp;&emsp;&emsp;&emsp;
            <input type="text" id="age" />
            <br/>
            <span>Gender:</span>&emsp;&emsp;&emsp;&emsp;&ensp;
            <input type="radio" name="gender" id="male" />
            <label for="male">Male</label>
            <input type="radio" name="gender" id="female" />
            <label for="female">Female</label>
            <br/>
            <p>For what purpose(s) you are making account?</p>
            <input type="checkbox" id="app" name="purpose" value="storingapps" />
            <label for="app">Storing Apps</label>
            <input type="checkbox" id="site" name="purpose" value="storingsites" />
            <label for="site">Storing Sites</label>
            <input type="checkbox" id="fun" name="purpose" value="fun" />
            <label for="fun">Fun</label>
            <br/>
            <input type="submit" value="Submit" class="button" />
        </form>
    </center>
</div>
<script>
    var labelsarray = document.getElementsByTagName("label");
    var inputsarray = document.getElementsByTagName("input");
    function submit() {
        for (var i = 0; i < inputsarray.length - 1; i++) {
            localStorage.setItem(labelsarray[i].innerHTML, inputsarray[i].value);
        }
    }
</script>

My form data is not storing in local storage of browser after submission please help me out.When I click on submit button it doesn't store data on local storage. When I remove function it works but to catch value of input I need to put it in a function.

Upvotes: 0

Views: 63

Answers (1)

jaredrethman
jaredrethman

Reputation: 511

To fix your immediate code. Change the name of your callback to something other then submit. submit() is reserved, source - https://www.w3schools.com/js/js_reserved.asp.

Upvotes: 1

Related Questions