Copy text when clicking on a DIV [duplicate]

1

How do I, with JQuery preferably, click on the wave class div:

$('ola').click(function(){
   //FUNÇÃO DE COPIAR AQUI
});

Does it copy a text? For example, when you click on the wave class div, it copies the text "Hello World"

    
asked by anonymous 10.05.2017 / 13:15

1 answer

1

It's pretty simple, and you do not necessarily need JQuery. There is a native function that does this:

$('#ola').click(function(){
        //Visto que o 'copy' copia o texto que estiver selecionado, talvez você queira colocar seu valor em um txt escondido
    $('#seuTxt').select();
    try {
            var ok = document.execCommand('copy');
            if (ok) { alert('Texto copiado para a área de transferência'); }
        } catch (e) {
        alert(e)
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><inputid="seuTxt" value="Olá mundo!" />
<input type="button" id="ola" value="Clique em mim" />

After clicking the button, try to give CTRL + V somewhere, it will paste the text

    
10.05.2017 / 13:54