views:

66

answers:

1

How can I select the contents of the whole page using jQuery for later to be copied to clipboard and hence another WYSIWYG.

The case is:

$("#SelectAll").click(function(){
//CODE TO SELECT ALL THE CONTENTS OF THE CURRENT PAGE
/* PS:
$("body").focus();
$("body").select(); //doesn't work 
*/
});

Any help is appreciated.

Thanks

FOUND THE SOLUTION:

function selectAll()
  var e = document.getElementsByTagName('BODY')[0];
  var r = document.createRange();
  r.selectNodeContents(e);
  var s = window.getSelection();
  s.removeAllRanges();
  s.addRange(r);
}

This works in FF haven't tested in other browsers. Just need to call selectAll wherever I want.

+1  A: 
if ('createRange' in document && 'getSelection' in window) {
    // firefox, opera, webkit
    var range= document.createRange();
    range.selectNodeContents(document.body);
    var selection= window.getSelection();
    selection.removeAllRanges();
    selection.addRange(range);
} else if ('createTextRange' in document.body) {
    // ie
    document.body.createTextRange().select();
}
bobince
Thanks for the answer.
GeekTantra