-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
81 lines (64 loc) · 2.53 KB
/
Copy pathscript.js
File metadata and controls
81 lines (64 loc) · 2.53 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
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
// 1. Element Selectors
const projectForm = document.getElementById('projectForm');
const projectNameInput = document.getElementById('projectName');
const projectTechSelect = document.getElementById('projectTech');
const tableBody = document.getElementById('tableBody');
// Error Selectors
const nameError = document.getElementById('nameError');
const techError = document.getElementById('techError');
// 2. Event Listener for Form Submission
projectForm.addEventListener('submit', function (event) {
// Prevent the default browser page refresh behavior
event.preventDefault();
// Fetch values and clear whitespace trailing ends
const nameValue = projectNameInput.value.trim();
const techValue = projectTechSelect.value;
let isValid = true;
// 3. Form Validation Logic
if (nameValue === '') {
nameError.textContent = 'Project Title is required.';
isValid = false;
} else {
nameError.textContent = '';
}
if (techValue === '') {
techError.textContent = 'Please select a core technology platform.';
isValid = false;
} else {
techError.textContent = '';
}
// 4. DOM Manipulation: If Valid, Add to Table View
if (isValid) {
addProjectToTable(nameValue, techValue);
// Form Reset utility
projectForm.reset();
}
});
// 5. Function to dynamically build and insert structural elements
function addProjectToTable(name, tech) {
// Create container row element
const row = document.createElement('tr');
// Create custom structural columns
const nameCell = document.createElement('td');
nameCell.textContent = name;
const techCell = document.createElement('td');
techCell.textContent = tech;
const statusCell = document.createElement('td');
statusCell.innerHTML = `<span class="status-badge">Active</span>`;
const actionCell = document.createElement('td');
const deleteBtn = document.createElement('button');
deleteBtn.textContent = 'Dismiss';
deleteBtn.className = 'action-btn';
// Attach click cleanup listener instance straight to item row target
deleteBtn.addEventListener('click', function () {
row.remove();
});
actionCell.appendChild(deleteBtn);
// Append table record elements explicitly to the wrapper row container
row.appendChild(nameCell);
row.appendChild(techCell);
row.appendChild(statusCell);
row.appendChild(actionCell);
// Inject row sequence element records into active UI body view
tableBody.appendChild(row);
}