Created
November 30, 2016 20:24
-
-
Save htmelvis/db5ac2f272a99d0f1e1d486854915c4c to your computer and use it in GitHub Desktop.
Select Text From Div on Click (jQuery)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function selectText(containerid) { | |
if (document.selection) { | |
var range = document.body.createTextRange(); | |
range.moveToElementText(document.getElementById(containerid)); | |
range.select(); | |
} else if (window.getSelection) { | |
var range = document.createRange(); | |
range.selectNode(document.getElementById(containerid)); | |
window.getSelection().addRange(range); | |
} | |
} | |
$('.copy-input').on('click', function(e){ | |
if(e.target.id){ | |
selectText(e.target.id); | |
var link = $(this).text(); | |
var copied = document.execCommand("copy", link); | |
if(copied){ | |
$(this).css( "border-color", "green"); | |
$( this ).after('<span class="copied-indicator">Copied!</span>'); | |
setTimeout(function(){ | |
$('.copied-indicator').fadeOut(); | |
//$('.copied-indicator').remove(); | |
$(this).css("border-color", "#777"); | |
}, 1400); | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment