-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
117 lines (112 loc) · 3.56 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// edit, add and delete blogs
function validateForm() {
var title = document.getElementById("title").value;
var description = document.getElementById("description").value;
var photo = document.getElementById("photo").value;
// Get the input element
if (title == "") {
alert("a title is required");
return false;
}
if (description == "") {
alert("please enter a description!");
return false;
}
if (photo == "") {
alert("enter image url ");
return false;
}
return true;
}
// function to show data
function showData() {
var blogs;
if (localStorage.getItem("blogs") == null) {
blogs = [];
} else {
blogs = JSON.parse(localStorage.getItem("blogs"));
}
var html = "";
blogs.forEach(function (element, index) {
html += "<tr>";
html += "<td>" + element.title + "</td>";
html += "<td>" + element.description + "</td>";
html += '<td ><button onclick="update(' + index + ')">Edit </button></td>';
html += '<td ><button onclick="deleteD(' + index + ')">Delete</button></td>';
/*html +=
'<td ><button id="confirmButton">Delete</button></td>';*/
html += "</tr>";
});
var answer= document.querySelector("#blogstable tbody");
answer.innerHTML = html;
}
//shows all data when page loads
document.onload = showData();
function Add() {
if (validateForm() == true) {
var title = document.getElementById("title").value;
var description = document.getElementById("description").value;
var photo = document.getElementById("photo").value;
var blogs;
if (localStorage.getItem("blogs") == null) {
blogs = [];
} else {
blogs = JSON.parse(localStorage.getItem("blogs"));
}
blogs.push({
title: title,
description: description,
photo: photo,
});
localStorage.setItem("blogs", JSON.stringify(blogs));
showData();
document.getElementById("title").value = "";
document.getElementById("description").value = "";
document.getElementById("photo").value = "";
}
}
function deleteD() {
var comfirmation = confirm("Do you want to delete this blog?");
if (comfirmation) {
deleteData();
}
}
function deleteData(index) {
var blogs;
if (localStorage.getItem("blogs") == null) {
blogs = [];
} else {
blogs = JSON.parse(localStorage.getItem("blogs"));
}
blogs.splice(index, 1);
localStorage.setItem("blogs", JSON.stringify(blogs));
showData();
}
function update(index) {
document.getElementById("Submit").style.display = "none";
document.getElementById("update").style.display = "block";
var blogs;
if (localStorage.getItem("blogs") == null) {
blogs = [];
} else {
blogs = JSON.parse(localStorage.getItem("blogs"));
}
document.getElementById("title").value = blogs[index].title;
document.getElementById("description").value = blogs[index].description;
document.getElementById("photo").value = blogs[index].photo;
document.querySelector("#update").onclick = function () {
if (validateForm() == true) {
blogs[index].title = document.getElementById("title").value;
blogs[index].description = document.getElementById("description").value;
blogs[index].photo = document.getElementById("photo").value;
localStorage.setItem("blogs", JSON.stringify(blogs));
showData();
document.getElementById("title").value = "";
document.getElementById("description").value = "";
document.getElementById("photo").value = "";
//hide update and return to add
document.getElementById("Submit").style.display = "block";
document.getElementById("update").style.display = "none";
}
}
}