-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJSONRemoveNoCSS.htm
140 lines (92 loc) · 2.76 KB
/
JSONRemoveNoCSS.htm
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form name = "form1" method = "post">
<input type = "text" name = "txt" placeholder = "Enter category" >
<input type = "button" id ="myBtn" name = "mybtn" value="Add it" >
<input type = "text" name = "prod" placeholder = "Enter Product">
<input type = "button" id = "Btn" name = "Btn" value = "Add it">
<br>
</br>
<input type = "button" id = "reBtn" name = "reBtn" value = "Remove It">
<br>
</br>
</form>
<table id = "tab1" ></table>
<script type="text/javascript">
var data =[{"category":"Automobile","products":"LandRover"},
{"category":"Pharma","products":"Maserati"},
{"category":"Fashion","products":"vintage"}];
var tab1 = document.getElementById("tab1");
function JsonData(){
for( var i =0 ; i<data.length ; i++)
{
var tabRow = document.createElement("tr");
tabRow.id = "trow" + i;
var tabDat1 = document.createElement("td");
tabDat1.innerHTML = data[i].category;
btn = document.createElement("button");
text1 = document.createTextNode("Remove");
btn.id = "btn" + i;
btn.addEventListener("click", rem)
btn.appendChild(text1);
tabRow.appendChild(btn);
for( var j =0; j < data.length; j ++)
{
var tabDat2 = document.createElement("td");
tabDat2.innerHTML = data[j].products;
tabDat1.appendChild(tabDat2);
tabRow.appendChild(tabDat1);
}
tab1.appendChild(tabRow);
}
}
JsonData();
function recreateTable()
{
var t1 = document.getElementById("tab1");
document.body.removeChild(t1);
tab1 = document.createElement("table");
tab1.id = "tab1";
document.body.appendChild(tab1);
JsonData();
}
function remove(){
for(var i = 0 ; i <data.length; i ++){
var trw = document.getElementById("trow"+i);
tab1.removeChild(trw);
}
}
function rem(){
for(var i = 0 ; i <data.length; i++)
{
if(event.target.id == "btn"+i)
{
var tw = document.getElementById("trow"+i);
tab1.removeChild(tw);
}
}
}
//Add category
document.getElementById("myBtn").addEventListener("click", function addCat(){
var new_data = document.form1.txt.value;
var new_data1 = "";
data.push({"category":new_data,"products":new_data1})
recreateTable();
})
//Add Products
document.getElementById("Btn").addEventListener("click", function addPr(){
var new_data2 = document.form1.prod.value;
var new_data3 = "";
data.push({"products":new_data2});
recreateTable();
})
//Remove category & products
document.getElementById("reBtn").addEventListener("click", function reBT(){
remove();
});
</script>
</body>
</html>