-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex-basic.js
55 lines (48 loc) · 2.07 KB
/
index-basic.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
// The following function represents the whole html as a single object
document.addEventListener('DOMContentLoaded', function() {
// Declare HTML objects to operate with them in JS
const title = document.getElementById('title'); // casi todo en JS se declara como constante
const description = document.getElementById('description');
const table = document.getElementById('table');
const alert = document.getElementById('alert');
const btn = document.getElementById('add');
let id = 1;
function removeTodo(id) {
console.log(id);
document.getElementById(id).remove();
}
btn.onclick = addTodo;
function addTodo() { // this function is declared inside the outer funcion
if (title.value === '' || description.value === '') { // in JS, === compares both DATA and TYPE OF DATA
alert.classList.remove('d-none'); // make visible this alert
alert.innerText = 'Title and description are required'; // change the text to be displayed
return;
}
// In case everything goes right...
alert.classList.add('d-none'); // make invisible this alert
const row = table.insertRow();
row.setAttribute('id', id++);
// insert the text into the table directly
row.innerHTML = `
<td>${title.value}</td>
<td>${description.value}</td>
<td class="text-center">
<input type="checkbox">
</td>
<td class="text-right">
<button class="btn btn-primary mb-1">
<i class="fa fa-pencil"></i>
</button>
</td>
`;
// REMOVE BUTTON
const removeBtn = document.createElement('button');
removeBtn.classList.add('btn', 'btn-danger', 'mb-1', 'ml-1');
removeBtn.innerHTML = '<i class="fa fa-trash"></i>';
removeBtn.onclick = function (e) {
removeTodo(row.getAttribute('id'));
}
row.children[3].appendChild(removeBtn);
}
btn.onclick = addTodo; // assignation of the function
});