1

Please help me solve this code. I've been fixing this for a month. Thank you for helping!

function copyText(text) {
	text.select();
	try {
		document.execCommand('copy');
	} catch (err) {
		console.log('Unable to copy' + err);
	}
}

copyText('JS is love');

1 Answer 1

2
  1. The .select() function call doesn't belong to strings but instead HTMLInputElement such as TextArea
  2. document.execCommand('copy') can only run as a result of an user action. In other words, it must belong inside an EventListener such as 'click'

Please refer to How do I copy to the clipboard in JavaScript? for more details

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.