I'm trying to write some code to simplify copying just the number from a bit of code that generates a mix of letters and a decimal value. I've so far been successful using a regex (my first time using one so if it's not perfect that's why), to simplify the data down to just the decimal number.
I now want the decimal number to be copied to the clipboard, to make things easier for the user. I've tried using the deprecated document.execCommand('copy') and navigator.clipboard.writeText(myvariable). This is how my function currently looks like, with a commented out graveyard of attempts.
In practice files, I can run implement copying to clipboard on button clicks for text that doesn't change, but it doesn't seem to like getting the value from the variable. As I've said in a previous post, Javascript is still new to me, so it's likely just a simple beginner error.
const copy = document.getElementById("copyPair")
copy.addEventListener("click", function(){
console.log("copy clicked");
const amount = document.getElementById("pair-price").innerText;
let decimal = Number(amount.replace(/[^0-9\.]+/g,""));
alert(decimal);
console.log(decimal);
/* decimal.select();
decimal.setSelectionRange(0, 99999); /!* For mobile devices *!/
navigator.clipboard.writeText(decimal.value);
let text = decimal;
navigator.clipboard.writeText(decimal.value);
document.execCommand('copy');
Any and all help getting copying the variable implemented would be very much appreciated!