Thanks for the answer. I have now got it working. However, I am unsure of how to modify it slightly.
I have the text box that shortens the characters onchange. But I then have to click the button to get it to copy to the clipboard.
This is OK but how can I combine both together so I no longer have the button and when I enter the number into the box, it automatically runs the script to shorten the characters and copies at the same time?
<input onchange="remove(this.value)" type="text" id="mytext" />
<button onclick="CopyFunction()">Copy Consignment No.</button>
<script>
document.getElementById('mytext').focus();
function remove(val) {
document.querySelector("#mytext").value = val.slice(6, -3);
}
function CopyFunction() {
/* Get the text field */
var copyText = document.getElementById("mytext");
/* 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>
To explain a bit better, the original number would be for example:
ACF3 460120121553025001 and I needed to take off the 1st 6 characters and the last 3 - so would be
60120121553025
I changed your start and end to “6 and 20” and it works great!
One last question… Is there a way to clear result in the text box after it has copied?
This would be so that I don’t duplicate the number the next time I use the script?
Create another function that references document.getElementById(“mytext”); and inserts no value in it. So after the alert in your code above, call that function with this in it:
...
alert("Copied the text: " + copyText.value);
clearField;
}
function clearField() {
document.getElementById('mytext').innerHTML = '"";
}