I have a table where I can add text to it. every row is stored in an array.
1 row = 1 array
the table is a 2d array.
var table = [[1,2,3,4,5], [9,9,9,9,9]];
Look at this picture:
the first row is table[0]
[1,2,3,4,5]
and second is table1
[9,9,9,9,9]
Now I have a button that allow me to add 5 elements to the table (1 new row).
And this is added to the array table[] with a push. So if I add 5 things to my array. For example numbers 7,7,7,7,7. It looks like this:
table = [[1,2,3,4,5], [9,9,9,9,9], [7,7,7,7,7]];
Now, I have a delete button, that allows me to delete rows in the table.
This is my code. And it deletes the row my button is on:
var tableCell = document.createElement("td");
var newButton = tableCell.appendChild(document.createElement("button"));
var buttonText = document.createTextNode("Remove");
// THIS FUNCTION DELETES THE ROW
newButton.onclick = function() {
//BEFORE I DELETE THE ROW, I WANT TO DELETE THE INDEX IN THE ARRAY TOO
//...........
(this).closest('tr').remove();
};
newButton.appendChild(buttonText);
...
Now I wonder, how can I also delete the row from my array? I must some how find the row number I am on, and delete it from my array.
If i am on row number 2 in picture (9,9,9,9,9) which is index 1.
And I click "remove", i want it to delete index 1 in my array.
But how can I find what index to remove? I tried rowIndex but I don't know how to make it work with this...
table.splice(INDEX OF ROW,1)
how can I find what row my button I click on, is located on? And then use that to do something like:
table.splice(1,1)
this removes 1 array from index 1 in my 2d array