-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommentBox.js
60 lines (43 loc) · 2.21 KB
/
commentBox.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
// First function to set the data in the form and return the data for display
function commentBox(){
let data = "";
let name = document.getElementById("userName").value
let email = document.getElementById("userEmail").value
let comment = document.getElementById("userComment").value
let count = textContent = textarea.value.length;
data = "<h5>User name: </h5>"+ name +"<h5>User email: </h5>"+email+ "<h5>Comment: </h5>"+comment + "<h5>Comment characters: </h5>" + count + " characters"
document.getElementById("data").innerHTML = data
}
// Now setting variable for the max number of characters, and querySelector to fetch the characters.
// Later I used the addEventListener to display the number of characters as they increase.
// Using JS object Method this to refer to the global object
// Using the .lenght array method to measure the text character lenght.
const max = 140;
let textarea = document.querySelector('textarea');
let info = document.querySelector('#info');
let counter = info.textContent = max - textarea.value.length;
textarea.addEventListener('input', function() {
info.textContent = max - this.value.length;
})
// New addEventListener to call function warningBox as characters change.
// Use JS DOM to get characters leght through another method.
// Using the if and else conditional statements to set the custom styling of the comment box and warning message.
textarea.addEventListener('input',warningBox)
function warningBox(){
let warning;
let count = document.getElementById('userComment').value.length;
if (count > max) {
warning = "Do not exceed 140 characters please",
document.getElementById("userComment").style.color = "red";
document.getElementById("userComment").style.border = "2px solid red";
} else {
warning = ""
document.getElementById("userComment").style.color = "black";
document.getElementById("userComment").style.border = "1px solid black";
}
document.getElementById("warningBox").style.color = "red";
document.getElementById("warningBox").innerHTML = warning;
console.log(warning);
console.log(count);
console.log(info);
}