Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Delete table row with JavaScript?

I am adding some data with JavaScript with no problem but I wanted to delete a row by adding delete button inside the row which I add, but I couldn’t do it. The code is below. The delete button doesn’t work:

const ad = document.querySelector("#ad");
const soyad = document.querySelector("#soyad");
const yas = document.querySelector("#yas");
const ekle = document.querySelector("#ekle");
const liste = document.querySelector("#liste");

ekle.onclick = function() {
  let tAd = document.createElement("td");
  let tSoyad = document.createElement("td");
  let tYas = document.createElement("td");
  let tSil = document.createElement("td");
  let silBtn = document.createElement("button");
  silBtn.textContent = "Sil";
  tAd.textContent = ad.value;
  tSoyad.textContent = soyad.value;
  tYas.textContent = yas.value;
  let tr = document.createElement("tr");
  tr.appendChild(tAd);
  tr.appendChild(tSoyad);
  tr.appendChild(tYas);
  tr.appendChild(silBtn);

  liste.appendChild(tr);
  ad.value = "";
  soyad.value = "";
  yas.value = "";
  ad.focus();

}
silBtn.onclick = function(e) {
  tr.appendChild(tAd);
  tr.appendChild(tSoyad);
  tr.appendChild(tYas);
  tr.appendChild(silBtn);
  liste.removeChild(this.parentNode.parentNode);
}
<div id="sayfa">
  <label for="">Ad:</label>
  <input type="text" id="ad">
  <label for="">Soyad</label>
  <input type="text" id="soyad">
  <label for="">Yas</label>
  <input type="text" id="yas">
  <button id="ekle">Tabloya Ekle</button>
  <table id="liste">
    <tr>
      <th>Ad</th>
      <th>SoyAd</th>
      <th>Yaş</th>
      <th>Sil</th>
    </tr>
  </table>
</div>

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Use This Javascript

<script>
const ad=document.querySelector("#ad");
const soyad=document.querySelector("#soyad");
const yas=document.querySelector("#yas");
const ekle=document.querySelector("#ekle");
const liste=document.querySelector("#liste");

ekle.onclick=function(){  
let tAd=document.createElement("td");
let tSoyad=document.createElement("td");
let tYas=document.createElement("td");
let tSil = document.createElement("td"); 
let silBtn =document.createElement("button");
silBtn.textContent="Sil";
tAd.textContent=ad.value;
tSoyad.textContent=soyad.value;
tYas.textContent=yas.value;
let tr=document.createElement("tr");
tr.appendChild(tAd);
tr.appendChild(tSoyad);
tr.appendChild(tYas);
tr.appendChild(silBtn);

liste.appendChild(tr);
ad.value="";
soyad.value="";
yas.value="";
ad.focus();
silBtn.onclick=function(e){ 
  liste.removeChild(this.parentNode); 
      }  
}
</script>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading