Ofir Ohayon
Ofir Ohayon

Reputation: 367

How to store form values on localStorage?

I need to make a JqueryMobile app for college work, i have that form -

<form name="local_storage_form" method="post" action="#" data-ajax="false" autocomplete="on" data-  theme="e" style="padding: 10px;" onsubmit="save_data()">


     <label for="id">identity card number</label><input type="text" name="id" id="id" placeholder="identity card number..." data-theme="e" required class="localStore">
     <label for="hphone">Home Phone Number</label><input type="tel" name="hphone" id="hphone" placeholder="Home Phone Number..." data-theme="e" class="localStore">
     <label for="mphone">Mobile Phone Number</label><input type="text" name="mphone" id="mphone" placeholder="Mobile Phone Number..." data-theme="e" required class="localStore" oninvalid="setCustomValidity('Invaild Phone Number (05********)')">
     <label for="bdate">Birth Date</label><input type="date" name="bdate" placeholder="Birth Date..." id="bdate" data-theme="e" required class="localStore">
     <label for="email">Email Address</label><input type="email" name="email" id="email" autocomplete="off" placeholder="Email Address..." data-theme="e" required class="localStore">
     <button type="button" value="Save" id="Save" data-theme="a">Submit</button>
     </form>

i need to store all the details on localstorage and get them after, how can i do it when i click on the submit button?

btw,it's an offline website so when i click submit it gives me an error.

Upvotes: 1

Views: 1451

Answers (1)

Ben Crowhurst
Ben Crowhurst

Reputation: 8481

Have a look into the Jquery API documentation.

$('#local_storage_form').submit(function(e) {
    e.preventDefault(); // prevent the form from attempting to send to the web server
    var $inputs = $('#local_storage_form :input');

    var values = {};
    $inputs.each(function() {
        values[this.name] = $(this).val();
    });

    localStorage.setItem('testForm', JSON.stringify(values));
});

Upvotes: 1

Related Questions