-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
50 lines (40 loc) · 1.7 KB
/
script.js
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
38
39
40
41
42
43
44
45
46
47
48
49
50
"use strict";
let inputText = document.getElementById('input-text');
let buttonUpperCase = document.getElementById('upper-case');
let buttonLowerCase = document.getElementById('lower-case');
let buttonProperCase = document.getElementById('proper-case');
let buttonSentenceCase = document.getElementById('sentence-case');
let buttonSaveTextFile = document.getElementById('save-text-file');
buttonUpperCase.addEventListener("click", () => {
inputText.value = inputText.value.toUpperCase();
});
buttonLowerCase.addEventListener("click", () => {
inputText.value = inputText.value.toLowerCase();
});
buttonProperCase.addEventListener("click", () => {
let textArray = inputText.value.toLowerCase().split(' ');
for (let i = 0; i < textArray.length; i++) {
textArray[i] = textArray[i].charAt(0).toUpperCase() + textArray[i].slice(1);
}
inputText.value = textArray.join(' ');
});
buttonSentenceCase.addEventListener("click", () => {
let textArray = inputText.value.toLowerCase().split('. ');
for (let i = 0; i < textArray.length; i++) {
textArray[i] = textArray[i].charAt(0).toUpperCase() + textArray[i].slice(1);
}
inputText.value = textArray.join('. ');
});
buttonSaveTextFile.addEventListener("click", () => {
function download(filename, text) {
let element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
// Start file download.
download("text.txt", inputText.value);
});