Kopiee mit einen klick einen Text in der Zwischenablage
Code
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<style>
body { background-color: #f5f5f5; }
.contact {
background-color: #fff;
padding: 15px;
max-width: 200px;
margin: 30px auto;
}
.icon {
width: 16px;
height: 16px;
padding: 0;
margin: 0;
vertical-align: middle;
}
</style>
<label>Kopiere den Text in de Zwischenablage</label>
<hr>
<div>
<textarea>Tippe ein Text ein den du kopieren willst und klicke dann den Button</textarea><button>Kopiere den Text</button>
</div>
<script>
function copyToClipboard(text, el) {
var copyTest = document.queryCommandSupported('copy');
var elOriginalText = el.attr('data-original-title');
if (copyTest === true) {
var copyTextArea = document.createElement("textarea");
copyTextArea.value = text;
document.body.appendChild(copyTextArea);
copyTextArea.select();
try {
var successful = document.execCommand('copy');
var msg = successful ? 'Copied!' : 'Whoops, not copied!';
el.attr('data-original-title', msg).tooltip('show');
} catch (err) {
console.log('Oops, unable to copy');
}
document.body.removeChild(copyTextArea);
el.attr('data-original-title', elOriginalText);
} else {
// Fallback if browser doesn't support .execCommand('copy')
window.prompt("Copy to clipboard: Ctrl+C or Command+C, Enter", text);
}
}
$(document).ready(function() {
$('button').click(function() {
var text = $('textarea').val()
var el = $(this);
copyToClipboard(text, el);
});
});
</script>