I have a problem with my implementation of a button meant to add functionality to a piece of code. It seems to be working, but why do the Date and Checkbox elements not behave like the ones in the first row?
It should calculate the difference in the date and cross the other <td>
. I need to call the Date
and Checkbox
properties with my add button.
My code is below. How can I fix it? Thank you for your time.
<!DOCTYPE html>
<html>
<head>
<title></title>
<style>
table {
border-collapse: collapse;
margin: 100px;
}
table,
td,
th {
border: 1px solid black;
padding: 10px;
}
table td.crossed {
background-image: -webkit-linear-gradient(top left, transparent, red, transparent);
background-image: -moz-linear-gradient(top left, transparent, red, transparent);
background-image: -o-linear-gradient(top left, transparent, red, transparent);
background-image: linear-gradient(to bottom right, transparent, red, transparent);
}
</style>
</head>
<body>
<table id="t1">
<tr>
<th></th>
<th></th>
<th></th>
<th></th>
<th>
<input style="margin-top:-200px; padding:10px" type="button" value="Add Row" onclick="addRow()" />
</th>
</tr>
<tr>
<td>1.</td>
<td>
<input type="checkbox" />
</td>
<td>
<input type="date" id="mydate">
</td>
<td contenteditable='true'></td>
<td>
<input type="button" value="Delete Row" onclick="deleteRow(this)" />
</td>
</tr>
</table>
<script>
function createRowColumn(row) {
var column = document.createElement("td");
row.appendChild(column);
return column;
}
function addRow() {
var newrow = document.createElement("tr");
var numericColumn = createRowColumn(newrow);
var checkColumn = createRowColumn(newrow);
var dateColumn = createRowColumn(newrow);
var emptyColumn = createRowColumn(newrow);
var removeColumn = createRowColumn(newrow);
var checkbox = document.createElement("input");
checkbox.setAttribute("type", "checkbox");
checkColumn.appendChild(checkbox);
var datebox = document.createElement("input");
datebox.setAttribute("type", "date");
dateColumn.appendChild(datebox);
emptyColumn.setAttribute("contenteditable", "true");
var remove = document.createElement("input");
remove.setAttribute("type", "button");
remove.setAttribute("value", "Delete Row");
remove.setAttribute("onClick", "deleteRow(this)");
removeColumn.appendChild(remove);
var table = document.getElementById('t1');
var tbody = table.querySelector('tbody') || table;
var count = tbody.getElementsByTagName('tr').length;
numericColumn.innerText = count.toLocaleString() + '.';
tbody.appendChild(newrow);
}
function deleteRow(button) {
var row = button.parentNode.parentNode;
var tbody = row.parentNode;
tbody.removeChild(row);
var rows = tbody.getElementsByTagName("tr");
for (var i = 1; i < rows.length; i++) {
var currentRow = rows[i];
currentRow.childNodes[0].innerText = i.toLocaleString() + '.';
}
}
var cb = document.querySelectorAll('input[type="checkbox"]')[0];
var td = document.querySelectorAll("td[contenteditable]")[0];
cb.addEventListener("click", function () {
if (cb.checked) td.classList.add("crossed");
else td.classList.remove("crossed");
});
window.onload = function () {
document.getElementById('mydate').onchange = function () {
var selectedDateFromCalendar = new Date(this.value);
var currentdate = new Date();
var Diff = new Date(selectedDateFromCalendar) - currentdate;
var diffDays = Math.ceil(Diff / (1000 * 3600 * 24));
if ((selectedDateFromCalendar) - currentdate < 0) {
alert("out of date");
}
else if ((selectedDateFromCalendar) - currentdate >= 1) {
alert("last " + diffDays + " day");
}
}
}
</script>
</body>
</html>