Last active
May 27, 2022 18:37
-
-
Save lewiswalsh/22518aa0192f62f88fca3f23632e77d0 to your computer and use it in GitHub Desktop.
Select and copy the contents of an element in Vue #vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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