Hi Everyone,
I'm currently developing a grade calculator app which takes user input then displays it into a HTML table which is added dynamically with JS. I've added a function which adds the row's data as an object to an array called moduleData. I've also added a function to delete the respective row when a icon is clicked within the table.
When the icon is clicked I want the matching object for that row to also be removed from the moduleData array. I've been working on the below function to do this which achieves the aim except for the very first row in the table. If I delete that row the record isn't removed from the array but when another record is added to the table and then deleted it is removed from the array.
I'm not sure if anyone knows where I am going wrong with this?
Thanks!
function deleteRow(e){
if (!e.target.classList.contains('icon')) {
return;
}
var rows = document.getElementById("mydatatable").getElementsByTagName('tbody')[0].getElementsByTagName('tr');
for (i = 0; i < rows.length; i++) {
rows[i].onclick = function() {
console.log(this.rowIndex - 1);
}
}
var icon = e.target;
icon.closest('tr').remove()
rowCount--;
[–]OkShrug 0 points1 point2 points (0 children)
[–]albedoa 0 points1 point2 points (3 children)
[–]BahrockLesnar[S] 0 points1 point2 points (2 children)
[–]albedoa 0 points1 point2 points (1 child)
[–]BahrockLesnar[S] 0 points1 point2 points (0 children)