Home > database >  copy button for input field and add URL automatic when its coped
copy button for input field and add URL automatic when its coped

Time:12-05

i have input field and copy button, visitors can write their phone number inside the input (ex: 123456789) and then click copy button to copy the numbers.

i want the visitor when click copy and past it anywhere it should be with URL example.com/123456789 so the URL add automatically and without ( ) mark.

i have this code but it does not work as i expect and i tried to modified still not work.

<input type="text" id="phonenumber">

<div >
<button type="button" onclick="myFunction()" onm ouseout="outFunc()">
  <span  id="myTooltip">Copy to clipboard</span>
  Copy text
  </button>
</div>
<script>
function myFunction() {
  var copyText = document.getElementById("phonenumber");
  copyText.select();
  copyText.setSelectionRange(0, 99999);
  navigator.clipboard.writeText(copyText.value);
  
  var tooltip = document.getElementById("myTooltip");
  tooltip.innerHTML = "Copied";
}

function outFunc() {
  var tooltip = document.getElementById("myTooltip");
  tooltip.innerHTML = "Copy to clipboard";
}
</script>

i've tried to do it like this var copyText = document.getElementById("example.com/","phonenumber"); But its not working, i add extra var but still not working

CodePudding user response:

Here's the working example of the code:

Codepen link (click to view live example)

Used string.substring(1) to remove the first character from the input text. Then simply added it to clipboard by appending example.com to it while writing it to clipboard

  • Related