-
-
Save TimonVS/c818655ce7ce260c6bdcdf05e33a7319 to your computer and use it in GitHub Desktop.
Download input values to a txt file
This file contains 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
const textInputElements = document.querySelectorAll('input[type="text"]'); | |
const inputValues = nodeListToArray(textInputElements) | |
.filter((x) => !!x.value) | |
.map((x) => [getFirstLabelForInput(x), x.value].join(' ')); | |
const text = inputValues.join('\n'); | |
const blob = new Blob([text], { | |
type: 'text/plain', | |
}); | |
downloadBlob(blob, 'text.txt'); | |
function getFirstLabelForInput(el) { | |
const labels = nodeListToArray(el.labels ?? []); | |
return labels?.[0]?.textContent ?? 'Unlabeled'; | |
} | |
function nodeListToArray(nodeList) { | |
return [...nodeList]; | |
} | |
function downloadBlob(blob, fileName) { | |
const url = window.URL.createObjectURL(blob); | |
const a = document.createElement('a'); | |
a.style.display = 'none'; | |
a.href = url; | |
a.download = fileName; | |
document.body.appendChild(a); | |
a.click(); | |
window.URL.revokeObjectURL(url); | |
} |
This file contains 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
const inputs = document.querySelectorAll('input'); | |
const inputValues = [...inputs].filter(x => !!x.value).map(x => x.value); | |
const text = inputValues.join('\n'); | |
const blob = new Blob([text], { | |
type: 'text/plain' | |
}); | |
downloadBlob(blob, 'text.txt'); | |
function downloadBlob(blob, fileName) { | |
const url = window.URL.createObjectURL(blob); | |
const a = document.createElement('a'); | |
a.style.display = 'none'; | |
a.href = url; | |
a.download = fileName; | |
document.body.appendChild(a); | |
a.click(); | |
window.URL.revokeObjectURL(url); | |
} | |
[...[...document.querySelectorAll('input')][3].labels][0].textContent |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment