Monday, September 14, 2015

Select and Copy to Clipboard in jQuery

<script>
function autoSelect(elem) {
    var selection, range;
    if (window.getSelection) {
        selection = window.getSelection();
        range = document.createRange();
        range.selectNodeContents(elem);
        selection.removeAllRanges();
        selection.addRange(range);
    } else if (document.selection) { // IE
        selection = document.selection.createRange().text;
        range = document.body.createTextRange();
        range.moveToElementText(elem);
        range.select();
    }
document.execCommand('copy');
}

// Eksekusi di sini
$('#btn').on("click", function() {
    autoSelect($('#result')[0]);
});
</script>

No comments:

Post a Comment