简体   繁体   中英

How to delete the table row on button click

  var del = document.createElement('input');
                  del.type = 'button';
                  del.name = 'delll';
                  del.value = 'del';
                  del.onclick = function(){ };  




  var table = document.getElementById("myTableData");
                var tableBody = document.createElement('TBODY');
                table.appendChild(tableBody);

                   var tr = document.createElement('TR');
                   tableBody.appendChild(tr);

                       var td = document.createElement('TD');
                       td.width='175';
                       td.appendChild(el);
                       tr.appendChild(td);

                       var td = document.createElement('TD');
                       td.width='245';
                       td.appendChild(objLabel);
                       td.appendChild(objLabel2);
                       tr.appendChild(td);

                       var td = document.createElement('TD');
                       td.width='245';
                       td.appendChild(el_s);
                       td.appendChild(el_sm);
                       td.appendChild(el_sy);
                       tr.appendChild(td);

                       **var td = document.createElement('TD');
                       td.width='20';
                       td.appendChild(del);
                       tr.appendChild(td);**

                      myTableData.appendChild(table);

In the above code I have created a table dynamically and added NAME field GENDER field and DATE OF BIRTH field and DELETE BUTTON.

The code works like this: first the user has to enter details in NAME GENDER AND DOB, if the DELETE button is clicked then the columns containing fields are deleted from the table.
I have to insert the code for deletion here:

del.onclick = function(){ };  

Try this:

del.onclick = function(){
    var tr = document.createElement('TR');
    tr.parentElement.removeChild(tr);
}; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM