I've seen this kind of thing on countless sites for different things. I'm sorry if I don't know the name of it, but here is an example of it:

read-only textbox with button to copy

See where they have the textbox and it has their address string in it to be easily copied or copied by the copy button. I'm wondering how to do this in WordPress, or if it is possible within wordpress using javascript code?


Here is a simple example from w3 schools that uses a bit of JS to copy the value of a input field.

<input type="text" value="StackExchange WordPress" id="myInput">
<button onclick="myFunction()">Copy text</button>

<p>The document.execCommand() method is not supported in IE9 and earlier.</p>

function myFunction() {
  var copyText = document.getElementById("myInput");
  alert("Copied the text: " + copyText.value);

Source found here: w3schools

  • 1
    Not sure if my edit will be accepted, anyway the function itself as mentioned in this answer need some adjustment. Imho is some missing in that code to make it read-only. Adding this into the function also should make it complete: document.getElementById("myInput").readOnly = true; – Charles Mar 23 '18 at 5:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.