Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

How to create multiple copy-to-clipboard buttons?

stigwest

New Coder
Hi! I'm no expert in HTML/CSS, so I hope somebody can help. I'm trying to make an easy list of standard answers to use at work, in my service desk function. A copy function such as this would be excellent, but I cannot seem to get it to work with more than one sentence to be copied. When I add a new tag with the same script, with at different standard text, clicking the button for the first text only copies the second text. My code is as follows, any clues to help me? I'd like to make an extensive list, so any tips on how to make this in the most effective way would be very welcome :)

<!DOCTYPE html>
<html>

<head>
<style>
body {
margin: 100px;
}


button {
width: 70px;
height: 40px;
margin-top: -50px;
margin-left: -40px;
background-color: green;
color: white;
border-radius: 10px;
box-shadow: grey;
position: absolute;
}

#Standard1 {
margin-top: 0px;
margin-left: 50px;
}
#Standard2 {
margin-top: 40px;
margin-left: 50px;
}
h1 {
margin-left: -40px;
margin-top: -100px;
font-size: large;
</style>
</head>

<body>
<div id="Standard1">
This is my first standard text
</div>
<br />

<button onclick="copyText()">Copy</button>
<br />

<h1>THESE ARE MY STANDARD TEXTS:</h1><br />

<script>
function copyText() {

/* Copy text into clipboard */
navigator.clipboard.writeText
("This is my first standard text");
}
</script>

</body>
<body>
<div id="Standard2">
This is my second standard text
</div>
<br />

<button onclick="copyText()">Copy</button>
<br />

<script>
function copyText() {

/* Copy text into clipboard */
navigator.clipboard.writeText
("This is my second standard text");
}
</script>
</body>
</html>
 

Buy us a coffee!

Back
Top Bottom