Skip to content

Instantly share code, notes, and snippets.

@lewiswalsh
Last active May 27, 2022 18:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lewiswalsh/22518aa0192f62f88fca3f23632e77d0 to your computer and use it in GitHub Desktop.
Save lewiswalsh/22518aa0192f62f88fca3f23632e77d0 to your computer and use it in GitHub Desktop.
Select and copy the contents of an element in Vue #vue
<template>
<div id="app">
<input v-on:focus="$event.target.select()" ref="myelement" readonly v-model="mytext" />
<button v-on:click="copyElement">Copy</button>
</div>
</template>
<script>
export default {
data() {
return {
mytext : 'This text will be selected and copied'
};
},
methods: {
copy() {
this.$refs.myelement.focus();
document.execCommand('copy');
}
}
};
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment