I am trying to make a single button which copies text to the clipboard, but I am am having trouble. It is copying the wrong thing.
Essentially, I have a variable called my_fav_food
. Then I have a button called My Fav Food
. When I click this button, it calls the function copying_function
and parses in the my_fav_food
variable into the function. Then the function automatically copies the text to the clipboard.
<!DOCTYPE html>
<html>
<body>
<script>
var my_fav_food = "My fav food is pizza"
</script>
<button onclick="copying_function(my_fav_food)">My Fav Food</button>
<script>
function copying_function(string) {
string.select();
document.execCommand("string");
}
</script>
</body>
</html>