1

Hello Guys I want to copy input value yeah on button click But nothing work. Please Help!!

<script>
  function copyToClipboard(element) {
    var $temp = $("<input>");
    $("body").append($temp);
    $temp.val($(element).text()).select();
    document.execCommand("copy");
    $temp.remove();
  }
</script>   

<input type="text" class="form-control" name="myvalue"  id="myvalue" value="YEAH" readonly  />

<button class="btn btn-primary btn-block" onclick="copyToClipboard('#myvalue')">Copy myvalue</button>
Boussadjra Brahim
  • 42,672
  • 10
  • 53
  • 86

2 Answers2

7

function copyToClipboard() {
    var textBox = document.getElementById("myvalue");
    textBox.select();
    document.execCommand("copy");
}
<input type="text" class="form-control" name="myvalue"  id="myvalue" value="YEAH" readonly  />

     <button class="btn btn-primary btn-block" onclick="copyToClipboard()">Copy myvalue</button>
Sindhoor
  • 414
  • 2
  • 12
0

Code if you want to copy input text to ClipBoard.

//HTML
<input type="text" class="form-control" name="myvalue"  id="myvalue" value="YEAH" readonly  />
<button class="btn btn-primary btn-block" onclick="copyToClipboard();">Copy myvalue</button>


//SCRIPT For ClipBoard Copy
 <Script>
    function copyToClipboard() {
        var textBox = document.getElementById("myvalue");
        textBox.select();
        document.execCommand("copy");
    }
</Script>



//For local storage variable copy
<Script>
    function copyToStorage() {
        var textBox = document.getElementById("myvalue");
        if(typeof(Storage) !== "undefined") {

            var textBox_value=textBox.value;
            localStorage.setItem("textBox_value", textBox_value);
         } else {
               alert("Sorry your browser does not support this script");
           }
    }
</Script>

   // Also there one to use an element like hidden to store the value for same page life.
A.D.
  • 2,257
  • 2
  • 13
  • 24