Last active
June 8, 2021 16:55
-
-
Save maxbmx/5ce9b9429e79960307c02967fa5a1e79 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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