Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //SELECTALL AND COPY ELEMENT TEXT TO CLIPBOARD
- var SELECTOR = '.w3-code';
- var html_button = '<button type="button" class="btn_copy_code">Copy code to clipboard</button>';
- //$(SELECTOR).parent().append(html_button);
- $(SELECTOR).after(html_button);
- $('.btn_copy_code').click(function() {
- var selector = $(SELECTOR);
- //selectAll(selector);
- // Get selector index https://stackoverflow.com/questions/3578461/jquery-returning-the-number-eq-of-a-clicked-div?rq=1
- var index = $(this).index('.btn_copy_code');
- //console.log(index);
- //console.log(selector[index]);
- CopyToClipboard(selector[index]);
- $(this).eq(0).html("Copied");
- });
- $(SELECTOR).on('mouseup', function() {
- var sel, range;
- var el = $(this)[0];
- console.log($(this));
- if (window.getSelection && document.createRange) { //Browser compatibility
- sel = window.getSelection();
- if(sel.toString() == ''){ //no text selection
- window.setTimeout(function(){
- range = document.createRange(); //range object
- range.selectNodeContents(el); //sets Range
- sel.removeAllRanges(); //remove all ranges from selection
- sel.addRange(range);//add Range to a Selection.
- },1);
- }
- }else if (document.selection) { //older ie
- sel = document.selection.createRange();
- if(sel.text == ''){ //no text selection
- range = document.body.createTextRange();//Creates TextRange object
- range.moveToElementText(el);//sets Range
- range.select(); //make selection.
- }
- }
- });
- function selectAll(selector) {
- var sel, range;
- var el = selector[0];
- console.log(selector);
- if (window.getSelection && document.createRange) { //Browser compatibility
- sel = window.getSelection();
- if(sel.toString() == ''){ //no text selection
- window.setTimeout(function(){
- range = document.createRange(); //range object
- range.selectNodeContents(el); //sets Range
- sel.removeAllRanges(); //remove all ranges from selection
- sel.addRange(range);//add Range to a Selection.
- },1);
- }
- }else if (document.selection) { //older ie
- sel = document.selection.createRange();
- if(sel.text == ''){ //no text selection
- range = document.body.createTextRange();//Creates TextRange object
- range.moveToElementText(el);//sets Range
- range.select(); //make selection.
- }
- }
- }
- function CopyToClipboard(element) {
- //console.log(element);
- if (document.selection) {
- var range = document.body.createTextRange();
- range.moveToElementText(element);
- range.select().createTextRange();
- document.execCommand("copy");
- } else if (window.getSelection) {
- var range = document.createRange();
- range.selectNode(element);
- //remove all ranges https://stackoverflow.com/questions/51234160/javascript-copy-to-clipboard-works-only-once
- window.getSelection().removeAllRanges();
- window.getSelection().addRange(range);
- document.execCommand("copy");
- //alert("Text has been copied, now paste in the text-area")
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement