-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
101 lines (92 loc) · 2.93 KB
/
index.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Twitter - 280 Characters Limit
// Instagram - 2200 Characters Limit
// Facebook - 63,206 Characters Limit
// LinkedIn - 3000 Characters Limit
const myTextarea = document.getElementById('text')
let charCount = 0
let wordCount = 0
render()
check()
// TextArea Input count (dynamic)
myTextarea.addEventListener('input', () => {
const text = myTextarea.value
charCount = 0
wordCount = 0
if(text !== '')
wordCount = text.trim().split(/\s+/).filter(word => word !== '').length
for (let i = 0; i < text.length; i++) {
charCount++
}
render()
check()
})
// Clear Button
document.getElementById('clear').addEventListener('click', () => {
myTextarea.value = ''
charCount = 0
wordCount = 0
render()
})
// Render Function
function render() {
let html = ``
html += `<p>Word Count: ${wordCount}</p>`
html += `<p>Character Count: ${charCount}</p>`
let social_html = ``
// Twitter
social_html += `
<div class="social">
<img src="./img/icons8-twitter-circled.svg" alt="Twitter" />
<p> <span id=twitter>${charCount}</span> / 280</p>
</div>
`
// Instagram
social_html += `
<div class="social">
<img src="./img/icons8-instagram.svg" alt="Instagram" />
<p><span id="instagram">${charCount}</span> / 2200</p>
</div>
`
// Facebook
social_html += `
<div class="social">
<img src="./img/icons8-facebook.svg" alt="Facebook" />
<p><span id="facebook">${charCount}</span> / 63206</p>
</div>
`
// LinkedIn
social_html += `
<div class="social">
<img src="./img/icons8-linkedin.svg" alt="LinkedIn" />
<p><span id="linkedin">${charCount}</span> / 3000</p>
</div>
`
document.getElementById('output').innerHTML = html
document.getElementById('social-output').innerHTML = social_html
}
function check(){
// Twitter
if(document.getElementById('twitter').textContent > 280) {
document.getElementById('twitter').style.color = 'red'
} else {
document.getElementById('twitter').style.color = 'green'
}
// Instagram
if(document.getElementById('instagram').textContent > 2200) {
document.getElementById('instagram').style.color = 'red'
} else {
document.getElementById('instagram').style.color = 'green'
}
// Facebook
if(document.getElementById('facebook').textContent > 63206) {
document.getElementById('facebook').style.color = 'red'
} else {
document.getElementById('facebook').style.color = 'green'
}
// LinkedIn
if(document.getElementById('linkedin').textContent > 3000) {
document.getElementById('linkedin').style.color = 'red'
} else {
document.getElementById('linkedin').style.color = 'green'
}
}