Posted under » JavaScript on 1 Dec 2023
This refer to my Ajax live search article.
Rather than select and copy you can copy by pressing a button
<script>
function kopy() {
// get the container
const element = document.querySelector('#livesearch');
// Create a fake `textarea` and set the contents to the text
// you want to copy
const storage = document.createElement('textarea');
storage.value = element.innerHTML;
element.appendChild(storage);
// Copy the text in the fake `textarea` and remove the `textarea`
storage.select();
storage.setSelectionRange(0, 99999);
document.execCommand('copy');
element.removeChild(storage);
}
</script>
<body>
<form>
<input type="text" size="30" onkeyup="showResult(this.value)">
<p><div id="livesearch"></div>
<div>
<button onclick="kopy()">Copy Me</button>
</div>
</form>