JavaScript realizes one click Copy of input box content (Vue 3 implementation method is attached)

Time:2021-10-17

1、 JS implementation mode

Implementation code:

One click Copy
const inputText = document.getElementsByTagName("input")[0];
const copyButton = document.getElementsByTagName("button")[0];

copyButton.addEventListener("click", () => {
  inputText.select();
  document.execCommand("copy");
});

2、 Vue3 implementation

Implementation code:

One click Copy
const inputText = ref({} as unknown);
const clickCopy = () => {
  (inputCode.value as HTMLInputElement).select();
  document.execCommand("copy");
};