Skip to content

Instantly share code, notes, and snippets.

@dangh
Last active October 3, 2017 03:39
Show Gist options
  • Save dangh/2d2eb5e4c65714eefa9f87251c8369d4 to your computer and use it in GitHub Desktop.
Save dangh/2d2eb5e4c65714eefa9f87251c8369d4 to your computer and use it in GitHub Desktop.
Replace <input/> tag with <textarea/> tag to preserve line-breaks.
/**
* Convert <input/> to <textarea/> to preserve line-breaks.
*/
function replaceInputWithTextarea(input) {
if (input.type !== 'text') return;
// Create textarea element.
const textarea = document.createElement('textarea');
// Copy all input attributes except `value`.
[].slice.apply(input.attributes).forEach(({nodeName, nodeValue}) => {
if (nodeName !== 'value') {
textarea.setAttribute(nodeName, nodeValue);
} else {
// Copy input value to textarea content.
textarea.innerHTML = nodeValue;
}
});
// Replace input tag.
input.replaceWith(textarea);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment