-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd_items.html
45 lines (44 loc) · 1.25 KB
/
add_items.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dynamic List</title>
<style>
body {
font-family: Arial, sans-serif;
}
#itemList {
margin-top: 20px;
}
.list-item {
margin: 5px 0;
}
</style>
</head>
<body>
<h1>My Item List</h1>
<input type="text" id="newItem" placeholder="Enter new item" />
<button onclick="addItem()">Add Item</button>
<ul id="itemList">
<li class="list-item">Item 1</li>
<li class="list-item">Item 2</li>
<li class="list-item">Item 3</li>
</ul>
</body>
<script>
function addItem() {
var newItem = document.getElementById('newItem').value;
if (newItem) {
var ul = document.getElementById('itemList');
var li = document.createElement('li');
li.className = 'list-item';
li.appendChild(document.createTextNode(newItem));
ul.appendChild(li);
document.getElementById('newItem').value = '';
} else {
alert('Please enter an item.');
}
}
</script>
</html>