Skip to content

Instantly share code, notes, and snippets.

@yakimelon
Created November 1, 2019 16:23
Show Gist options
  • Save yakimelon/6e7b047523bf12fa43935de45dfac556 to your computer and use it in GitHub Desktop.
Save yakimelon/6e7b047523bf12fa43935de45dfac556 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>test</title>
</head>
<body>
<form>
<textarea id="text-area" cols="30" rows="10"></textarea>
<button id="save-button">保存</button>
<button id="remove-button">削除</button>
</form>
<script>
(() => {
'use strict';
let formArea = document.getElementById('form-area');
let textArea = document.getElementById('text-area');
let saveBtn = document.getElementById('save-button');
let rmvBtn = document.getElementById('remove-button');
let textData = textArea.value;
let storageData = localStorage.getItem('data');
window.onload = () => {
if(!textArea.value) {
let parseData = JSON.parse(storageData);
textArea.value = parseData;
}
}
const saveText = () => {
event.preventDefault();
textData = textArea.value;
return textData;
};
const sendStorage = () => {
saveText();
console.log(textData);
localStorage.setItem('data', JSON.stringify(textData));
};
const removeText = () => {
textArea.value = '';
localStorage.removeItem('data', JSON.stringify(textData));
};
saveBtn.addEventListener('click', sendStorage, false);
rmvBtn.addEventListener('click', removeText, false);
})();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment