Last active
October 3, 2017 03:39
-
-
Save dangh/2d2eb5e4c65714eefa9f87251c8369d4 to your computer and use it in GitHub Desktop.
Replace <input/> tag with <textarea/> tag to preserve line-breaks.
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
/** | |
* 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