Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicohaemhouts/0fa3ea89cad0b7d77648 to your computer and use it in GitHub Desktop.
Save nicohaemhouts/0fa3ea89cad0b7d77648 to your computer and use it in GitHub Desktop.
Pasting multi-line text into a single-line text input in Internet Explorer
<input type="text" name="myTextValue" alue="">

Pasting multi-line text into a single-line text input in Internet Explorer

When you paste multi-line text into a single-line text input in Internet Explorer you only get the first line. This is because IE chucks away everything after the first newline-character. Other browsers will simply replace the newline with spaces. This snippet does the same thing for IE. If window.clipboardData exists we're on IE and we should intercept the paste event to replace newlines with spaces.

A Pen by Nico Haemhouts on CodePen.

License.

if (window.clipboardData) {
$('input[type=text]').on('paste', function (e) {
e.preventDefault();
$(this).val(window.clipboardData.getData('Text').replace(/(\r\n|\n|\r)/gm, " "));
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment