forked from lsvekis/JavaScript-Exercises-Book
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamic Bookmark Manager
More file actions
53 lines (53 loc) · 1.48 KB
/
Dynamic Bookmark Manager
File metadata and controls
53 lines (53 loc) · 1.48 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
47
48
49
50
51
52
53
<!-- Objective: Create a dynamic bookmark manager. -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Bookmark Manager</title>
</head>
<body>
<input type="text" id="bookmarkInput" placeholder="Add a new bookmark URL...">
<button id="addBookmark">Add Bookmark</button>
<ul id="bookmarkList"></ul>
<script>
const bookmarkInput = document.getElementById('bookmarkInput');
const addBookmarkButton = document.getElementById('addBookmark');
const bookmarkList = document.getElementById('bookmarkList');
let bookmarks = JSON.parse(localStorage.getItem('bookmarks')) || [];
function renderBookmarks() {
bookmarkList.innerHTML = '';
bookmarks.forEach((bookmark, index) => {
const li = document.createElement('li');
const a = document.createElement('a');
a.href = bookmark;
a.textContent = bookmark;
a.target = '_blank';
const removeButton = document.createElement('button');
removeButton.textContent = 'Remove';
removeButton.onclick = () => {
bookmarks.splice(index, 1);
updateLocalStorage();
renderBookmarks();
};
li.appendChild(a);
li.appendChild(removeButton);
bookmarkList.appendChild(li);
});
}
function addBookmark() {
const url = bookmarkInput.value;
if (url) {
bookmarks.push(url);
updateLocalStorage();
renderBookmarks();
bookmarkInput.value = '';
}
}
function updateLocalStorage() {
localStorage.setItem('bookmarks', JSON.stringify(bookmarks));
}
addBookmarkButton.addEventListener('click', addBookmark);
renderBookmarks();
</script>
</body>
</html>