Skip to content

Instantly share code, notes, and snippets.

@TimonVS
Last active January 13, 2022 09:43
Show Gist options
  • Save TimonVS/c818655ce7ce260c6bdcdf05e33a7319 to your computer and use it in GitHub Desktop.
Save TimonVS/c818655ce7ce260c6bdcdf05e33a7319 to your computer and use it in GitHub Desktop.
Download input values to a txt file
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);
}
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