2

I am trying to copy the background hex colour using JS. Is there a way to do this? I know there is .execCommand("copy"); but for some reason it's not working. Not sure what I am missing here:

var color = '';

$('.copy-button').click(function() {
    var x = $(this).css('backgroundColor');
    hexc(x);
    alert(color);
})

function hexc(colorVal) {
    var parts = colorVal.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
    delete(parts[0]);
    for (var i = 1; i <= 3; ++i) {
        parts[i] = parseInt(parts[i]).toString(16);
        if (parts[i].length == 1) parts[i] = '0' + parts[i];
    }
    color = '#' + parts.join('');
    document.execCommand("copy");
}

This is what I tried so far: http://jsfiddle.net/DCaQb/997/

brunodd
  • 2,474
  • 10
  • 43
  • 66

0 Answers0