Skip to content

Commit 404e298

Browse files
authored
Merge pull request tajulafreen#12 from olanikegloria/choice-picker
created the choice picker files
2 parents 61cefa8 + 571f59e commit 404e298

File tree

3 files changed

+134
-0
lines changed

3 files changed

+134
-0
lines changed

Source-Code/ChoicePicker/index.html

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<link rel="stylesheet" href="style.css">
7+
<title>Document</title>
8+
</head>
9+
<body>
10+
<div class="container">
11+
<h3>Enter all of the choices divided by a comma (',').
12+
<br>Press enter when you're done</h3>
13+
<textarea placeholder="enter choices here ..." id="textarea"></textarea>
14+
</div>
15+
16+
<div id="tags">
17+
<span class="tag">Choice 1 </span>
18+
<span class="tag ">Choice 2</span>
19+
<span class="tag">Choice 3</span>
20+
</div>
21+
22+
<script src="script.js"></script>
23+
</body>
24+
</html>

Source-Code/ChoicePicker/script.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
const tagsEl = document.getElementById('tags');
2+
const textarea = document.getElementById('textarea');
3+
4+
textarea.focus();
5+
6+
const createTags = (input) => {
7+
const tags = input.split(',').filter((tag) => tag.trim() !== '').map((tag) => tag.trim());
8+
tagsEl.innerHTML = '';
9+
tags.forEach((tag) => {
10+
const tagEl = document.createElement('span');
11+
tagEl.classList.add('tag');
12+
tagEl.innerText = tag;
13+
tagsEl.appendChild(tagEl);
14+
});
15+
};
16+
17+
const pickRandomTag = () => {
18+
const tags = document.querySelectorAll('.tag');
19+
return tags[Math.floor(Math.random() * tags.length)];
20+
};
21+
22+
const highlightTag = (tag) => {
23+
tag.classList.add('highlight');
24+
};
25+
26+
const unHighlightTag = (tag) => {
27+
tag.classList.remove('highlight');
28+
};
29+
30+
const randomSelect = () => {
31+
const times = 30;
32+
const interval = setInterval(() => {
33+
const randomTag = pickRandomTag();
34+
highlightTag(randomTag);
35+
36+
setTimeout(() => {
37+
unHighlightTag(randomTag);
38+
}, 100);
39+
}, 100);
40+
41+
setTimeout(() => {
42+
clearInterval(interval);
43+
44+
setTimeout(() => {
45+
const randomTag = pickRandomTag();
46+
47+
highlightTag(randomTag);
48+
}, 100);
49+
}, times * 100);
50+
};
51+
52+
textarea.addEventListener('keyup', (e) => {
53+
createTags(e.target.value);
54+
if (e.key === 'Enter') {
55+
setTimeout(() => {
56+
e.target.value = '';
57+
}, 10);
58+
randomSelect();
59+
}
60+
});

Source-Code/ChoicePicker/style.css

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
* {
2+
box-sizing: border-box;
3+
}
4+
5+
body {
6+
background-color: blueviolet;
7+
font-family: 'Roboto', sans-serif;
8+
display: flex;
9+
flex-direction: column;
10+
align-items: center;
11+
justify-content: center;
12+
height: 100vh;
13+
overflow: hidden;
14+
margin: 0;
15+
}
16+
17+
h3 {
18+
color: white;
19+
margin: 10px 0 20px;
20+
text-align: center;
21+
}
22+
23+
.container {
24+
width: 500px;
25+
}
26+
27+
textarea {
28+
border: none;
29+
display: block;
30+
width: 100%;
31+
height: 100px;
32+
font-family: inherit;
33+
padding: 10px;
34+
font-size: 16px;
35+
margin: 0 0 20px;
36+
}
37+
38+
.tag {
39+
background-color: #f0932b;
40+
color: white;
41+
border-radius: 50px;
42+
padding: 10px 20px;
43+
margin: 0 5px 10px 0;
44+
font-size: 14px;
45+
display: inline-block;
46+
}
47+
48+
.tag.highlight {
49+
background-color: plum;
50+
}

0 commit comments

Comments
 (0)