-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
76 lines (66 loc) · 2.64 KB
/
script.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
//Variables
var webTitleInput = document.querySelector("#title-input");
var webUrlInput = document.querySelector("#url-input");
var enterBtn = document.querySelector(".enter-button");
var bookmarksList = document.querySelector(".bookmarks-list");
var readBtn = document.querySelector("#read-button");
var deleteBtn = document.querySelector("#delete-button");
var errorMsg = document.querySelector(".error");
var outputRead = document.querySelector(".read-bookmarks-counter");
var outputUnread = document.querySelector(".unread-bookmarks-counter");
//Event Listeners
enterBtn.addEventListener("click", submit);
//Functions
function checkInput() {
if (webTitleInput.value.length !== 0 && webUrlInput.value.length !== 0) {
document.getElementById('enter').disabled = false;
errorMsg.innerText = '';
}
};
function submitInput() {
if (webTitleInput.value.length === 0) {
errorMsg.innerText = 'Please complete entire form';
document.getElementById('enter').disabled = true;
webTitleInput.addEventListener('keyup', checkInput);
} else if (webUrlInput.value.length === 0) {
errorMsg.innerText = 'Please complete entire form';
document.getElementById('enter').disabled = true;
webUrlInput.addEventListener('keyup', checkInput);
}
};
function submit(event) {
event.preventDefault();
var bookmarkEntry = `<section class="bookmark-background">
<article aria-label="bookmark">
<h3>${webTitleInput.value}</h3>
<section class="link-border">
<a href="http://${webUrlInput.value}" alt="Link to ${webTitleInput.value}">${webUrlInput.value}</a></section>
<button class="read-button" type="button" onclick="markRead(event)">Read</button>
<button class="delete-button" type="reset" onclick="markDelete(event)">Delete</button>
</article></section>`;
if (webTitleInput.value.length === 0 || webUrlInput.value.length === 0) {
submitInput();
} else {
bookmarksList.innerHTML += bookmarkEntry ;
}
postCountBookmarks();
};
function postCountBookmarks() {
var readCount = document.querySelectorAll('.read').length;
var totalBookmarks = document.querySelectorAll('.link-border').length;
var unreadCount = totalBookmarks - readCount;
outputRead.innerText = 'Total read bookmarks: ' + readCount;
outputUnread.innerText = 'Total unread bookmarks: ' + unreadCount;
};
function markRead(event) {
event.preventDefault();
var article = event.target.parentNode;
article.classList.toggle('read');
postCountBookmarks();
};
function markDelete(event) {
event.preventDefault();
var bookmark = event.target.parentNode;
bookmark.parentNode.parentNode.removeChild(bookmark.parentNode);
postCountBookmarks();
};