You can use the clipboard.js
library, click here to view your documentation .
Solution using clipboard.js
:
// Inicia o Clipboard no elemento "button"
var copiar = new Clipboard('button', {
// Define o texto a ser copiado
text: function(trigger) {
// Obtem "src" da "img" que está no mesmo nível do "button"
return $(trigger).siblings('img').attr('src');
}
});
// Retorna um alert se copiar com sucesso.
copiar.on('success', function(e) {
alert('Copiado para a area de transferencia!');
});
// Retorna um alert se copiar com erro.
copiar.on('error', function(e) {
alert('Houve um erro ou navegador não suportado');
});
<!-- CSS do Bootstrap, para ficar mais bonitinho :P -->
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<!-- JS do Jquery e do Clipboard.js (NECESSÁRIO!) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><scriptsrc="https://cdn.jsdelivr.net/clipboard.js/1.5.12/clipboard.min.js"></script>
<!-- Código com suas imagens! -->
<div class="col-xs-10 col-offset-2">
<div class="col-xs-5">
<img src="https://pixabay.com/static/uploads/photo/2015/08/20/14/00/frog-897419_960_720.jpg"class="img-thumbnail" alt="Cinque Terre" width="100%">
<button class="btn btn-block btn-primary">
COPIAR
</button>
</div>
<div class="col-xs-5">
<img src="https://pixabay.com/static/uploads/photo/2015/08/20/14/00/frog-897418_960_720.jpg"class="img-thumbnail" alt="Cinque Terre" width="100%">
<button class="btn btn-block btn-primary">
COPIAR
</button>
</div>
</div>
Other methods of use:
You can use clipboard.js
in a number of other ways, not limited to the posted demo, to complement I'll be posting some examples, which are also contained in the documentation site
Copy value of input
:
// Inicia o Clipboard no "button"
new Clipboard('button');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><scriptsrc="https://cdn.jsdelivr.net/clipboard.js/1.5.12/clipboard.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<div class="col-xs-10">
<div class="col-xs-8">
<!-- Alvo a ser copiado -->
<input id="id" class="form-control" value="Digite algo, ou clique em Copiar para copiar isso!">
</div>
<div class="col-xs-2">
<!-- Botão para copiar -->
<button class="btn btn-block" data-clipboard-target="#id">
Copiar
</button>
</div>
</div>
In this case, data-clipboard-target=""
is responsible for copying the value of another element, in this case input
. So when clicking the user will copy the written content in the element defined in data-clipboard-target
.
Copy pre-defined text in element:
// Inicia o Clipboard no "button"
new Clipboard('button');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><scriptsrc="https://cdn.jsdelivr.net/clipboard.js/1.5.12/clipboard.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<div class="col-xs-10">
<button class="btn btn-block" data-clipboard-text="Você copiou esse texto :O">
Copiar!
</button>
</div>
In this case, data-clipboard-text=""
is responsible for defining the text to be copied. So when clicking the user will copy the content written in the element data-clipboard-text
.
Compatibility:
Official library support list:
Chrome 42+
Mozilla 41+
Internet Explorer 9+
Opera 29+
License:
Images are under "Public Domain":
link
link
Clipboard.js is under "MIT License":
link