Skip to content

Instantly share code, notes, and snippets.

@csharpforevermore
Created May 25, 2021 10:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save csharpforevermore/5a2086624b81b0a7abfd5ccf17590daa to your computer and use it in GitHub Desktop.
Save csharpforevermore/5a2086624b81b0a7abfd5ccf17590daa to your computer and use it in GitHub Desktop.
function detach(element) {
return element.parentElement.removeChild(element);
}
function move(src, dest, isBefore) {
dest.insertAdjacentElement(isBefore ? 'beforebegin' : 'afterend', detach(src));
}
function children(element, selector) {
return element.querySelectorAll(selector);
}
function child(element, selector, index) {
return children(element, selector)[index];
}
function row(table, index) {
// Generic Version: return child(table.querySelector('tbody'), 'tr', index);
return table.querySelector('tbody').querySelector(`tr:nth-child(${index + 1})`);
}
function moveRow(table, fromIndex, toIndex, isBefore) {
move(row(table, fromIndex), row(table, toIndex), isBefore);
}
// useage Move "Alpha" (index = 2) to be before "Beta" (index = 0)
//moveRow(document.querySelector('table'), 2, 0, true);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment