Carriage returns removed from textarea when copied to clipboard
Hi guys,
I'm using the following script to copy the contents of a texarea to the clipboard. Seems to only work in IE but that's not an issue as this is a tool for work where everyone uses IE anyway. I'm working with IE9.
Code:
function highlightmetasearch01() {
document.copydata01.text2copy01.select(); document.copydata01.text2copy01.focus(); }
function copymetasearch01() {
highlightmetasearch01();
textRange = document.copydata01.text2copy01.createTextRange();
textRange.execCommand("RemoveFormat");
textRange.execCommand("Copy"); }
The above script is then called with the following markup:
Code:
<form name="copydata01">
Title of template
<button onclick="copymetasearch01();return false;">COPY</button>
<br />
<textarea name="text2copy01">Text with carriage
returns that should be
maintained but aren't!</textarea>
</form>
The problem is that when the text is copied, the carriage returns in the textarea are removed. So the above would output as:
Text with carriage returns that should be maintained but aren't!
...This never used to be a problem with older versions of IE.
The textareas I'm using aren't part of a form to be submitted to a server or email, just static text boxes for people to copy text from.
I tried using the attribute wrap="hard" in the textarea tag but that doesn't fix it (seems to control how the text is sent to a server - but my text isn't going to a server).
Tried re-entering the text manually, as it was originally pasted from a Word document - no joy.
I created an offline test page called test.html - it was just a rough check to see if a different copy-to-clipboard script would work, so I didn't bother putting in a doctype. Lo and behold, my templates pasted as they were formatted, maintaining carriage returns. I even renamed it as .php and uploaded it to my server, to emulate how my other files are. It worked.
Transferring this functionality to my website? Oh dear... no doctype means my external CSS either wouldn't load or wasn't being read properly. I'm thinking the latter, as I'm using a lot of CSS3.
So I put the doctype back in and tested again (withering, at the point of giving up the whole project), but this time accidentally pasting into Notepad++ - the carriage returns were maintained!!! I tried MS Word - maintained!!! As it turns out, I was testing the paste in Windows Notepad - it was removing the carriage returns!!!
Arrrrrrrggghhhhhh so much time wasted arrrrrrrggghhhhhh lol
Bookmarks