You can use the library clipboard.js
, click here to see 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>
<script src="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 the clipboard.js
in various other ways, not limited to the demonstration posted, to complement I will be posting some examples, which are also contained on the documentation website
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>
<script src="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 that case the data-clipboard-target=""
is responsible for copying the value of another element, in this case the input
. So by clicking the user will copy the content written in the element defined in data-clipboard-target
.
Copy pre-defined text into 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>
<script src="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 that case the data-clipboard-text=""
is responsible for defining the text to be copied. So 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+
Furlough:
Imagery are under "Public Domain":
https://pixabay.com/pt/sapo-adeus-viagens-bagagem-holdall-897420/
https://pixabay.com/pt/sapo-adeus-viagens-bagagem-holdall-897418/
Clipboard.js is under "MIT License":
https://zenorocha.mit-license.org/
your link will not work ederjc as it is on the localhost
– DiChrist
is just an example can be any url, and it works yes, when manually copying an img of src it appears normally.
– ederjc
Rsrs, when I said it won’t work, that’s when someone clicks there. This what you want to do I was seeing one of these days, if I find the reference, put here for you
– DiChrist
What do you want here : http://answall.com/questions/89139/como_copiar-para-%C3%A1rea-de-transfer%C3%Aancia-em-javascript, Additional Reading
– Guilherme Lautert
I understood thank you :D
– ederjc