0

i'm trying to copy a text to the clipboard. But've already shown the text as selected in the modal window where it appears after an ajax call.The code is the following:

jQuery.fn.selectText = function(){
var doc = document
    , element = this[0]
    , range, selection
;
if (doc.body.createTextRange) {
    range = document.body.createTextRange();
    range.moveToElementText(element);
    range.select();
} else if (window.getSelection) {
    selection = window.getSelection();        
    range = document.createRange();
    range.selectNodeContents(element);
    selection.removeAllRanges();
    selection.addRange(range);
}

so after range = document.createRange(); i tryied inserting range.execCommand('copy'); cause i've read this tutorial about it but it doesn't mention any problem with this command. The error i'm getting is the following:

TypeError: range.execCommand is not a function

This is a mozilla tutorial about execCommand.

Community
  • 1
  • 1
softwareplay
  • 1,379
  • 4
  • 28
  • 64
  • `execCommand` is a member of `document`. – marekful Feb 03 '14 at 16:25
  • Marcell is correct, "...the document object exposes the execCommand method which allows one to run commands to manipulate the contents of the editable region." – Matt K Feb 03 '14 at 16:28
  • So how can i apply the "copy" command to a specific text in javascript? – softwareplay Feb 03 '14 at 16:37
  • Actually IE's `TextRange` object, roughly equivalent to the DOM Range implemented in other browsers, has an `execCommand()` method, but it isn't usually useful. – Tim Down Feb 03 '14 at 21:14

1 Answers1

2

A range doesn't have an execCommand function, the execCommand function belongs to the document object.

Taken from the same tutorial:

When an HTML document has been switched to designMode, the document object exposes the execCommand method which allows one to run commands to manipulate the contents of the editable region. Most commands affect the document's selection (bold, italics, etc), while others insert new elements (adding a link) or affect an entire line (indenting). When using contentEditable, calling execCommand will affect the currently active editable element.

plalx
  • 42,889
  • 6
  • 74
  • 90