Skip to content
Advertisement

Copy more than one div to clipboard and adding characters

I am using the following script to copy a div to clipboard. But I am trying to copy multiple divs (DivA + DivB) with the same button, while adding some quotes and brackets around each div. I saw some answers (like this one, and this), but I can’t seem to be able to implement them to the current script.

So the output should be like:

JavaScript

This is the current script to copy one div.

JavaScript
JavaScript

Advertisement

Answer

The issue is because you’re only reading the text from ‘#divA’, as that’s the selector passed to the copyToClipboard() function.

To do what you require amend the logic to create a string in the format you require containing the text of both #divA and #divB:

JavaScript
JavaScript
JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement