File tree Expand file tree Collapse file tree 3 files changed +133
-0
lines changed Expand file tree Collapse file tree 3 files changed +133
-0
lines changed Original file line number Diff line number Diff line change
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 >
Original file line number Diff line number Diff line change
1
+ const tagsEl = document . getElementById ( 'tags' )
2
+ const textarea = document . getElementById ( 'textarea' )
3
+
4
+ textarea . focus ( )
5
+
6
+ function 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
+
18
+ function pickRandomTag ( ) {
19
+ const tags = document . querySelectorAll ( '.tag' )
20
+ return tags [ Math . floor ( Math . random ( ) * tags . length ) ]
21
+ }
22
+
23
+ function highlightTag ( tag ) {
24
+ tag . classList . add ( 'highlight' )
25
+ }
26
+
27
+ function unHighlightTag ( tag ) {
28
+ tag . classList . remove ( 'highlight' )
29
+ }
30
+
31
+ function randomSelect ( ) {
32
+ const times = 30
33
+ const interval = setInterval ( ( ) => {
34
+ const randomTag = pickRandomTag ( )
35
+ highlightTag ( randomTag )
36
+
37
+ setTimeout ( ( ) => {
38
+ unHighlightTag ( randomTag )
39
+ } , 100 )
40
+ } , 100 )
41
+
42
+ setTimeout ( ( ) => {
43
+ clearInterval ( interval )
44
+
45
+ setTimeout ( ( ) => {
46
+ const randomTag = pickRandomTag ( )
47
+
48
+ highlightTag ( randomTag )
49
+ } , 100 )
50
+ } , times * 100 )
51
+ }
52
+
53
+ textarea . addEventListener ( 'keyup' , ( e ) => {
54
+ createTags ( e . target . value )
55
+ if ( e . key === 'Enter' ) {
56
+ setTimeout ( ( ) => {
57
+ e . target . value = ''
58
+ } , 10 )
59
+ randomSelect ( )
60
+ }
61
+ } )
Original file line number Diff line number Diff line change
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
+ h3 {
17
+ color : white;
18
+ margin : 10px 0 20px ;
19
+ text-align : center;
20
+ }
21
+ .container {
22
+ width : 500px
23
+ }
24
+
25
+ textarea {
26
+ border : none;
27
+ display : block;
28
+ width : 100% ;
29
+ height : 100px ;
30
+ font-family : inherit;
31
+ padding : 10px ;
32
+ font-size : 16px ;
33
+ margin : 0 0 20px ;
34
+ }
35
+
36
+ .tag {
37
+ background-color : # f0932b ;
38
+ color : white;
39
+ border-radius : 50px ;
40
+ padding : 10px 20px ;
41
+ margin : 0 5px 10px 0 ;
42
+ font-size : 14px ;
43
+ display : inline-block;
44
+ }
45
+
46
+ .tag .highlight {
47
+ background-color : plum;
48
+ }
You can’t perform that action at this time.
0 commit comments