Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxbmx/5ce9b9429e79960307c02967fa5a1e79 to your computer and use it in GitHub Desktop.
Save maxbmx/5ce9b9429e79960307c02967fa5a1e79 to your computer and use it in GitHub Desktop.
let table = "<table><tbody>";
for (let li of document.querySelectorAll("li")) {
table += "<tr>";
table += getRowString(li, "");
table += "</tr>";
}
function getRowString(li, rowString) {
if (li.nodeName !== "LI") {
// Finish and return row if there is no more li's to parse
return rowString;
}
// Add current li's text as td before it's children text
rowString = `${getTdFromLi(li)}${rowString}`;
// Go up one element and repeat process using li's parent element
return getRowString(li.parentNode.parentNode, rowString);
}
function getTdFromLi(li) {
return `<td>${li.childNodes[0].nodeValue.trim()}</td>`;
}
table += "</tbody></table>";
/*
To print resulting table directly on page, use:
document.write(table);
To dump resulting table's code inside console, use:
console.log(table);
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment