-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBookmarks.js
More file actions
46 lines (39 loc) · 1.56 KB
/
Bookmarks.js
File metadata and controls
46 lines (39 loc) · 1.56 KB
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
// displays article if there is any
//adds article if it exists in local storage
let articles = JSON.parse(localStorage.getItem('articles')) || [];
let article_container = document.getElementById('saved_articles_container');
const empty_page = document.querySelector('.empty_page');
if (articles.length === 0) {
empty_page.style.display = `flex`;
}
else {
empty_page.style.display = `none`;
articles.forEach((article, index) => {
const temp_article = document.createElement('div');
temp_article.classList.add('article_item');
temp_article.innerHTML = `
<img src="${article.Image}" alt="article image">
<div class="article_item_text">
<h3>${article.title}</h3>
<p>${article.desc}</p>
</div>
<img src="resources/Iconify/trashcan.svg" alt="remove item icon" class="remove_icon" id="removeiconstyle" data-index="${index}">`
article_container.appendChild(temp_article);
});
//add the event listener for the delete icon
document.querySelectorAll('.remove_icon').forEach(icon => {
icon.addEventListener('click', (e) => {
const article_index = e.target.getAttribute('data-index');
removeArticle(article_index);
});
});
}
function removeArticle(index) {
articles.splice(index, 1);
localStorage.setItem('articles', JSON.stringify(articles));
const article_items = document.querySelectorAll('.article_item');
article_items[index].remove();
if (articles.length === 0) {
empty_page.style.display = `flex`;
}
}