简体   繁体   中英

JS IE Error (Object doesn't support property or method 'select')

Trying to copy some info to clipboard.

In Chrome and others works fine, In IE I get the error: "Object doesn't support property or method 'select'"

html:

<textarea name="wtBodyInpt" id="wtBodyInpt">Copy This</textarea>

JS:

function copyBodyToClipboard (BodyInpt) {
   // Select text inside element
   BodyInpt.focus();
   BodyInpt.select();
   // Copy text to clipboard
   document.execCommand('copy');
}
copyBodyToClipboard(wtBodyInpt)

You need to create an object of the element by accessing the element using its ID or name.

Example:

 <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> </head> <body> <p>Try to paste the text by pressing ctrl + v in a different window, to see the effect.</p> <textarea name="wtBodyInpt" id="wtBodyInpt">Copy This Text</textarea> <p>The document.execCommand() method is not supported in IE8 and earlier.</p> <script> function copyBodyToClipboard (element) { var copyText = document.getElementById(element); copyText.select(); copyText.setSelectionRange(0, 99999); document.execCommand("copy"); } var str="wtBodyInpt"; copyBodyToClipboard (str) </script> </body> </html>

Output in IE 11 browser:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM