Hugo Silva
Hugo Silva

Reputation: 209

Copy to clipboard html with back slash

I'm trying to create a button that when clicked will copy a text to clipboard.

<button onclick="copyToClipboard('A\B\C\D')">Click here</button>

but when using this approach, the text copied to the clipboard is:

ABCD

instead of

A\B\C\D

Can someone help here please?

Upvotes: 1

Views: 931

Answers (2)

Zeronull
Zeronull

Reputation: 271

\ is the JavaScript escape character. To use a backslash use \\.

Further reading: https://www.w3schools.com/js/js_strings.asp

Upvotes: 0

Borislav Aymaliev
Borislav Aymaliev

Reputation: 843

You can try:

<button onclick="copyToClipboard('A\\B\\C\\D')">Click here</button>

or

<button onclick="copyToClipboard('A/B/C/D')">Click here</button>

and see if any of the two will work for your task.

Upvotes: 3

Related Questions