Skip to content

Commit

Permalink
Merge pull request #14 from M-AminAlizadeh/setup/preserve-data-in-loc…
Browse files Browse the repository at this point in the history
…alstorge

Setup preserve data in localstorge and viceversa
  • Loading branch information
M-AminAlizadeh committed Apr 14, 2023
2 parents 610d291 + 2663f56 commit 9d65f25
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
5 changes: 4 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -598,14 +598,15 @@ <h2 class="form-header">Contact me</h2>
class="form-element"
placeholder="Enter your full-name"
maxlength="30"
id="form-full-name"
required />
<!-- Email -->
<input
name="form_email"
type="email"
class="form-element"
placeholder="Enter your email address"
id="email-input"
id="form-email"
required />
<!-- Message -->
<textarea
Expand All @@ -614,6 +615,7 @@ <h2 class="form-header">Contact me</h2>
rows="10"
placeholder="Write your message here..."
maxlength="500"
id="form-message"
required></textarea>
<div class="error-message-container">
<img
Expand All @@ -635,6 +637,7 @@ <h2 class="form-header">Contact me</h2>
</section>
<!-- Script -->
<script src="./js/menu-popup.js"></script>
<script src="./js/preserve-data.js"></script>
<script src="./js/validate-contact-form.js"></script>
<script src="./js/details-popup.js"></script>
</body>
Expand Down
23 changes: 23 additions & 0 deletions js/preserve-data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const nameInput = document.querySelector('#form-full-name');
const emailInput = document.querySelector('#form-email');
const messageInput = document.querySelector('#form-message');
const submitButton = document.querySelector('#submit-btn');
const userData = {
fullName: '', email: '', message: '',
};

// Fetch data from localstorage to UI
window.addEventListener('load', () => {
const DATA = JSON.parse(localStorage.getItem('userData'));
nameInput.value = DATA.fullName;
emailInput.value = DATA.email;
messageInput.value = DATA.message;
});

// Fetch data from UI to localstorage
submitButton.addEventListener('submit', () => {
userData.fullName = nameInput.value;
userData.email = emailInput.value;
userData.message = messageInput.value;
localStorage.setItem('userData', JSON.stringify(userData));
});

0 comments on commit 9d65f25

Please sign in to comment.