-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
116 lines (92 loc) · 3.53 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
var selectedIndex = -1;
function save() {
var name = document.getElementById('name').value
var mobile = document.getElementById('mobile').value
var email = document.getElementById('email').value
var btn = document.getElementById('btn').value
if(btn == "ADD")
{
var new_contact = {
"name": name,
"mobile" :mobile,
"email" :email
}
if(localStorage.getItem('mycontacts'))
{
var mycontacts = JSON.parse(localStorage.getItem('mycontacts'))
mycontacts.push(new_contact);
localStorage.setItem('mycontacts', JSON.stringify(mycontacts));
}
else{
var mycontacts = []
mycontacts.push(new_contact);
localStorage.setItem('mycontacts', JSON.stringify(mycontacts));
}
document.getElementById('result').innerHTML = "Contact added"
}
else{
var mycontacts = JSON.parse(localStorage.getItem('mycontacts'))
mycontacts[selectedIndex].name=name
mycontacts[selectedIndex].mobile=mobile
mycontacts[selectedIndex].email=email
localStorage.setItem('mycontacts', JSON.stringify(mycontacts));
alert("Data updated")
document.getElementById('btn').value = "ADD"
}
document.getElementById('name').value = ""
document.getElementById('mobile').value = ""
document.getElementById('email').value = ""
display()
}
function display(){
if(localStorage.getItem('mycontacts'))
{
var mycontacts = JSON.parse(localStorage.getItem('mycontacts'))
var temp = "<table id='space'>";
temp += "<tr>"
temp += "<td>Name</td>";
temp += "<td>Mobile</td>";
temp += "<td>Email Id</td>";
temp += "<th colspan='2'>Action</th>";
temp += "</tr>"
for(i=0; i<mycontacts.length; i++)
{
temp += "<tr>";
temp += "<td>"+mycontacts[i].name+"</td>";
temp += "<td>"+mycontacts[i].mobile+"</td>";
temp += "<td>"+mycontacts[i].email+"</td>";
temp += "<td><button onclick='editContact("+i+")'>Edit</button></td>";
temp += "<td><button onclick='deleteContact("+i+")'>Delete</button></td>";
temp += "</tr>";
}
temp += "</table>";
document.getElementById('list').innerHTML = temp;
}
else{
document.getElementById('list').innerHTML = "There are no contacts";
}
}
function editContact(index){
selectedIndex = index;
var mycontacts = JSON.parse(localStorage.getItem('mycontacts'))
document.getElementById('name').value = mycontacts[index].name;
document.getElementById('mobile').value = mycontacts[index].mobile;
document.getElementById('email').value = mycontacts[index].email;
document.getElementById('btn').value="UPDATE";
}
function deleteContact(index){
if(confirm("Are u sure to delete? "))
{
var mycontacts = JSON.parse(localStorage.getItem('mycontacts'));
var newContacts = [];
for(i=0;i<mycontacts.length;i++)
{
if(i!= index){
newContacts.push(mycontacts[i]);
}
}
localStorage.setItem('mycontacts', JSON.stringify(newContacts));
// alert(`deleted`);
display();
}
}