-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
61 lines (52 loc) · 1.61 KB
/
main.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
//Declaring variables to our html elements.
let form = document.getElementById("form");
let input = document.getElementById("inputs");
let message = document.getElementById("message");
let posts = document.getElementById("posts");
//Prevents the default behavior when the form is submitted.
form.addEventListener("submit", (e) => {
e.preventDefault();
console.log("button clicked");
//The formValidation function is invoked everytime the button is clicked.
formValidation();
});
//A simple function to ensure that no blank posts are submitted.
let formValidation = () => {
if (input.value === "") {
message.innerHTML = "Post cannot be blank";
console.log("failure");
} else {
console.log("success");
message.innerHTML = "";
acceptData();
}
};
//If the text area has a message, this function stores the message as an object.
let data = {};
let acceptData = () => {
data["text"] = input.value;
console.log(data);
createPost();
};
//function is used to create posts using the below HTMl format.
let createPost = () => {
posts.innerHTML += `
<div>
<p>${data.text}</p>
<span class="options">
<i onclick="editPost(this)" class="fas fa-edit"></i>
<i onclick = "deletePost(this)" class="fas fa-trash-alt"></i>
</span>
</div>
`;
input.value = "";
};
//function for deleting a post.
let deletePost = (e) => {
e.parentElement.parentElement.remove();
};
//function for editing a post.
let editPost = (e) => {
input.value = e.parentElement.previousElementSibling.innerHTML;
e.parentElement.parentElement.remove();
};