Sometimes, it’s helpful to copy and paste some text programmatically. There are different options in Javascript, and the only one that works consistently is the following:
You can see an example of the above code in action on Stackblitz here. The only caveat is that such code can be triggered from an event listener, such as onClick
.
A newer API is emerging but isn’t fully supported anywhere, and Firefox support is significantly lacking, so I wouldn’t recommend it. The main problem with that new approach is that it requires the user’s permission, and there’s currently no way to ask for it. The Permissions API seems to be a bit inconsistent and lacking at this point:
It’s a shame because that new API would allow copying custom content into the clipboard, not just the current user’s selection.
By the way, a quick reminder to tune in for the Angular live event today on Youtube.