Skip to content

Instantly share code, notes, and snippets.

@cthae
Last active February 27, 2022 07:03
Show Gist options
  • Save cthae/f84a50bcd765f74596ae3017c513d44e to your computer and use it in GitHub Desktop.
Save cthae/f84a50bcd765f74596ae3017c513d44e to your computer and use it in GitHub Desktop.
adding a row into a table
function addSource(){
const count = document.querySelector("table#mainSources").querySelectorAll("tr").length;
const newRow = document.querySelector("tr#tr-0").cloneNode(true);
newRow.id = "tr-"+count;
newRow.querySelectorAll("td").forEach(td => {
const name = td.getAttribute("name");
if (name == "alias" || name == "mID"){
td.querySelector("label").innerHTML = td.querySelector("label").innerHTML.replace(":", " " + count + ":");
td.querySelector("input").id = td.querySelector("input").getAttribute("id").replace("0",count);
td.querySelector("input").value = "";
}
})
document.querySelector("table#mainSources").appendChild(newRow);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment