-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsave-form.js
More file actions
37 lines (29 loc) · 1.08 KB
/
Copy pathsave-form.js
File metadata and controls
37 lines (29 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
const contactForm = document.getElementById('form');
const contactEmailField = document.getElementById('email');
const contactUsernameField = document.getElementById('name');
const contactMessageField = document.getElementById('textarea');
function isLocaleStorageAvailable() {
try {
const x = 'test__localStorage';
localStorage.setItem(x, x);
localStorage.removeItem(x);
return true;
} catch (err) {
return false;
}
}
window.addEventListener('load', () => {
if (!isLocaleStorageAvailable()) return;
const isDataExist = JSON.parse(localStorage.getItem('contact-form'));
if (!isDataExist) return;
contactUsernameField.value = isDataExist.username;
contactEmailField.value = isDataExist.email;
contactMessageField.value = isDataExist.message;
});
contactForm.addEventListener('input', () => {
if (!isLocaleStorageAvailable()) return;
const username = contactUsernameField.value;
const email = contactEmailField.value;
const message = contactMessageField.value;
localStorage.setItem('contact-form', JSON.stringify({ username, email, message }));
});