What's new
HTML Forums | An HTML and CSS Coding Community

Welcome to HTMLForums; home of web development discussion! Please sign in or register your free account to get involved. Once registered you will be able to connect with other members, send and receive private messages, reply to topics and create your very own. Our registration process is hassle-free and takes no time at all!

How to create copy to clipboard buttons?

stigwest

New member
Hi! 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>
 
Back
Top