xxxxxxxxxx
HTML:
<!-- The text field -->
<input type="text" value="Hello World" id="myInput">
<!-- The button used to copy the text -->
<button onclick="myFunction()">Copy text</button>
JAVASCRIPT:
function myFunction() {
/* Get the text field */
var copyText = document.getElementById("myInput");
/* Select the text field */
copyText.select();
copyText.setSelectionRange(0, 99999); /* For mobile devices */
/* Copy the text inside the text field */
navigator.clipboard.writeText(copyText.value);
/* Alert the copied text */
alert("Copied the text: " + copyText.value);
}
xxxxxxxxxx
HTML copy button with Bootstrap CSS
<!-- HTML -->
<div class="input-group mb-3">
<input type="text" class="form-control" disabled id="copyInput" aria-describedby="button-addon2" value="Hello World">
<button class="btn btn-outline-secondary" onclick="myFunction()" type="button" id="button-addon2">Copy Text</button>
</div>
<!-- JS -->
<script>
function myFunction() {
/* Get the text field */
var copyText = document.getElementById("copyInput");
/* Select the text field */
copyText.select();
copyText.setSelectionRange(0, 99999); /* For mobile devices */
/* Copy the text inside the text field */
navigator.clipboard.writeText(copyText.value);
/* Alert the copied text */
alert("Copied the text: " + copyText.value);
}
</script>
html copy button with Bootstrap