-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
36 lines (33 loc) · 1.15 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
const textElement = document.getElementById('text');
const selectElement = document.getElementById('set-select');
// Function to generate a random text from the JSON data
function generateText() {
fetch('texts.json')
.then(response => response.json())
.then(data => {
// Access the desired set of data using the selected option value
const textSet = data[selectElement.value];
// Generate a random text from the set
const text = textSet[Math.floor(Math.random() * textSet.length)];
// Update the text element with the generated text
textElement.textContent = text;
});
}
// Populate the select element with the available sets of data
fetch('texts.json')
.then(response => response.json())
.then(data => {
for (const key in data) {
const option = document.createElement('option');
option.value = key;
option.textContent = key;
selectElement.appendChild(option);
}
});
// Generate a text when the user press Enter or tap on mobile
document.addEventListener('keydown', event => {
if (event.key === 'Enter') {
generateText();
}
});
document.addEventListener('touchstart', generateText);