-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
58 lines (57 loc) · 2.08 KB
/
main.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
//declaring variables
var input = document.getElementById("inputx");
var btn = document.getElementById("btnx");
var list = document.getElementById("listx");
//adding button and input eventlisteners
btn.addEventListener("click", addItemv1);
input.addEventListener("keypress", addItemv2);
//button listener function
function addItemv1() {
if (input.value == "") {
alert("enter something...");
}
else {
var element = document.createElement("li");
element.appendChild(document.createTextNode(input.value));
list.appendChild(element);
var new_btn = document.createElement("button");
new_btn.appendChild(document.createTextNode("remove"));
list.appendChild(new_btn);
input.value = "";
new_btn.style.color = "red";
new_btn.style.borderColor = "black";
new_btn.style.fontSize = "16px";
new_btn.style.borderRadius = "15px";
new_btn.addEventListener("click", removeItem);
function removeItem() {
element.style.display = "none";
new_btn.style.display = "none";
}
}
}
//input listener function
function addItemv2(event) {
if (event.keyCode == 13) {
if (input.value == "") {
alert("enter something...");
}
else {
var element = document.createElement("li");
element.appendChild(document.createTextNode(input.value));
list.appendChild(element);
var new_btn = document.createElement("button");
new_btn.appendChild(document.createTextNode("remove"));
list.appendChild(new_btn);
input.value = "";
new_btn.style.color = "red";
new_btn.style.borderColor = "black";
new_btn.style.fontSize = "16px";
new_btn.style.borderRadius = "15px";
new_btn.addEventListener("click", removeItem);
function removeItem() {
element.style.display = "none";
new_btn.style.display = "none";
}
}
}
}