deleteRow Do not delete table row completely

Asked

Viewed 53 times

0

Follow my javascript function that should permanently delete a line:

function deletaItemExtra(item){

            var x = document.getElementById(item);

            x.deleteCell(1);

            x.deleteCell(0);

            document.getElementById(item).deleteRow;

            if(document.getElementById(item) != null){

                alert('Ainda é encontrado no sistema'); 

            }else{

                alert('Foi totalmente deletado do sistema'); 

            }

        }

The problem is that when I check the lines, which should have been deleted, it is still on the table. For this reason I put this check at the end. The table is also generated dynamically. Follows code:

var itemExtra   = itemExtra.split("|");
                var idExiste = false;
                var id = 0;
                while(idExiste == false){
                    id  = Math.floor((Math.random() * 10000) + 1) + '_' + itemExtra[0].replace(' ', '');
                    if(document.getElementById(id) == null){
                        idExiste = true;
                    }
                }
                var table       = document.getElementById(tabela);
                var row         = table.insertRow(0);
                row.id          = id;
                var cell1       = row.insertCell(0);
                var cell2       = row.insertCell(1);
                cell1.innerHTML = itemExtra[1];
                var deletaExtra = document.createElement("INPUT");
                deletaExtra.setAttribute("type", "button");

                deletaExtra.addEventListener("click", function(){
                    deletaItemExtra(id.replace(" ",""));
                });

                deletaExtra.setAttribute("value", 'X');
                cell2.appendChild(deletaExtra);

I searched and in some places said it could be on account of your daughters, so I’m deleting both. Some dirt?

  • First of all, the id should start with a letter, then you can use the number you want, but it can’t just be a number. Make this adjustment and see if it resolves, if not resolved, we proceed

  • Hello @Brunorigolon, I set this but it hasn’t worked yet.

1 answer

2

You are not calling the function. This:

document.getElementById(item).deleteRow

should be:

document.getElementById(item).deleteRow(0);

or simply:

x.deleteRow(0);

I’m passing zero, which represents the first row of the table, because I don’t know which row you want to delete. But you need to pass the index of the line to be removed.

  • Hello @bfavaretto. Really it was wrong. But it still didn’t work. Shouldn’t it receive some parameter within the parentheses? I tried with 0 but it didn’t work. Now he’s stopping the script on this line.

  • 1

    Yes, you need to pass the index of the line you want to remove. 0 is the first line, 1 is the second, etc.

  • 1

    That’s assuming item be the table.

  • Then, the item is the id of Row. as it would return only one line, it would be the item 0 of the Indice. But if I need to go through the whole table, I need to change my script.

  • 1

    So do it @Matheusdelatorrre: x.parentElement.removeChild(x)

  • Perfect! Thank you. I was already doing a go through the table.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.