-
Notifications
You must be signed in to change notification settings - Fork 163
Expand file tree
/
Copy pathscript.js
More file actions
47 lines (36 loc) · 1.49 KB
/
script.js
File metadata and controls
47 lines (36 loc) · 1.49 KB
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
const initialData = {
"Name": "Muthu raman",
"Email": "muthuraman@2002.com",
"profession":"Developer"
};
document.addEventListener('DOMContentLoaded', () => {
const userInfoList = document.getElementById('userInfoList');
// Populate initial data
for (const [key, value] of Object.entries(initialData)) {
addListItem(key, value);
}
});
document.getElementById('userForm').addEventListener('submit', function(event) {
event.preventDefault(); // Prevent form submission
const fieldName = document.getElementById('fieldName').value;
const fieldValue = document.getElementById('fieldValue').value;
addListItem(fieldName, fieldValue);
// Clear input fields
document.getElementById('fieldName').value = '';
document.getElementById('fieldValue').value = '';
});
function addListItem(name, value) {
const userInfoList = document.getElementById('userInfoList');
const listItem = document.createElement('li');
listItem.innerHTML = `${name}: ${value} <button class="edit-button" onclick="editEntry(this)">Edit</button>`;
userInfoList.appendChild(listItem);
}
function editEntry(button) {
const listItem = button.parentElement;
const text = listItem.textContent.replace(" Edit", "").trim();
const [fieldName, fieldValue] = text.split(": ");
document.getElementById('fieldName').value = fieldName;
document.getElementById('fieldValue').value = fieldValue;
// Remove the entry from the list
listItem.remove();
}