let vergitcnoElement = document.createElement('td'); vergitcnoElement.innerHTML =…
- User
let vergitcnoElement = document.createElement('td'); vergitcnoElement.innerHTML = vergitcno; vergitcnoElement.setAttribute('class', 'tableInfoToDelete'); document.getElementById('vergiNoTable').insertAdjacentElement('afterend', vergitcnoElement);
let satisismiElement = document.createElement('td'); satisismiElement.innerHTML = satisismi; satisismiElement.setAttribute('class', 'tableInfoToDelete'); document.getElementById('satisIsmiTable').insertAdjacentElement('afterend', satisismiElement); let plakaDegisikligiElement = document.createElement('td'); plakaDegisikligiElement.innerHTML = plakaDegisikligi; plakaDegisikligiElement.setAttribute('class', 'tableInfoToDelete'); document.getElementById('plakaDegisTable').insertAdjacentElement('afterend', plakaDegisikligiElement); let aracSatisTuruElement = document.createElement('td'); aracSatisTuruElement.innerHTML = aracSatisTuru; aracSatisTuruElement.setAttribute('class', 'tableInfoToDelete'); document.getElementById('kullanimAmaciTable').insertAdjacentElement('afterend', aracSatisTuruElement); let satisTuruElement = document.createElement('td'); satisTuruElement.innerHTML = satisTuru; satisTuruElement.setAttribute('class', 'tableInfoToDelete'); document.getElementById('satisTipi').insertAdjacentElement('afterend', satisTuruElement);Can you shorten this code
Assistant
Certainly! You can shorten the code by creating a function that handles the creation and insertion of the elements, reducing repetition. Here is a more concise version of the code:
function createAndInsertCell(id, content) { let element = document.createElement('td'); element.innerHTML = content; element.setAttribute('class', 'tableInfoToDelete'); document.getElementById(id).insertAdjacentElement('afterend', element); } createAndInsertCell('vergiNoTable', vergitcno); createAndInsertCell('satisIsmiTable', satisismi); createAndInsertCell('plakaDegisTable', plakaDegisikligi); createAndInsertCell('kullanimAmaciTable', aracSatisTuru); createAndInsertCell('satisTipi', satisTuru);This function
createAndInsertCelltakes theidof the element after which the new cell should be inserted and thecontentthat the cell should contain. It then creates the cell, sets the content and class, and inserts it into the document in one go. Each call tocreateAndInsertCellreplaces a block of the original repetitive code.