Duplicating labels with JavaScript

I need assistance with copying HTML to my clipboard. The issue I am encountering is that when I try to copy the button inside the tagHolder, it ends up copying

<span style="font-family: Arial; font-size: 13.3333px; text-align: center; background-color: rgb(221, 221, 221);">
instead of the desired
<button>button</button>
. In Chrome, I would like it to copy the innerHTML of tagHolder.

function $id(id) {
    return document.getElementById(id);
}

function performCopy() {
var tagHolder = $id('tagHolder');
    tagHolder.style.visibility = '';
    initiateCopying(tagHolder)
    tagHolder.style.visibility = 'hidden';
}

function displayCode() {
    var code = $id("xxx").innerHTML;
    $id("txtinfo").innerText = code;
}

function initiateCopying(element) {
    if (document.selection) {
        var range = document.body.createTextRange();
range.moveToElementText(element);
        range.select();
    } else if (window.getSelection) {
        var range = document.createRange();
range.selectNodeContents(element);
        window.getSelection().removeAllRanges();
        window.getSelection().addRange(range);
    }
    document.execCommand("Copy");
}
<!DOCTYPE html>
<html>
<body>
    <button onclick="performCopy()">copy</button><br />

    paste:<br />
    <div id="xxx" contenteditable="true" onpaste="setTimeout(displayCode, 1);" style="border: 1px solid black;"></div>
    html:<br />
    <div id="txtinfo" style="border: 1px solid black;"></textarea>

    <div id="tagHolder" style="visibility: hidden;"><button>button</button></div>

</body>

</html>

Answer №1

Learn how to use the Clipboard.js library by following the demonstration in David Walsh's blog post

function $id(id) {
  return document.getElementById(id);
}

function doCopy() {
  var clipboard = new ClipboardJS('.tagHolder');
}

function showCode() {
  var code = $id("xxx").innerHTML;
  $id("txtinfo").innerText = code;
}

function $id(id) {
  return document.getElementById(id);
}

function doCopy() {
  var clipboard = new ClipboardJS('.tagHolder');
}

function showCode() {
  var code = $id("xxx").innerHTML;
  $id("txtinfo").innerText = code;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.0/clipboard.min.js"></script>

<button onclick="doCopy()">copy</button><br /> paste:

<br />
<div id="xxx" contenteditable="true" onpaste="setTimeout(showCode, 1);" style="border: 1px solid black;"></div>
html:<br />
<div id="txtinfo" style="border: 1px solid black;"></div>

<div class="tagHolder" data-clipboard-action="copy" data-clipboard-target="#xxx" style="visibility: hidden;"><button>button</button></div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Pressing the "Enter" key will submit the contents of

Hello, I have recently created a new chat box and everything seems to be working fine. However, I am facing an issue with submitting a message when I press enter (to go to the function Kucaj()). Can anyone provide assistance with this problem? I tried ad ...

"Utilize the power of the ajaxform jQuery plugin to automatically reset form fields

Initially, I'd like to emphasize that this is an original inquiry. My predicament is as follows: I have a chatroom php script where I utilize the ajaxForm jQuery plugin to seamlessly send new messages without having to reload the entire page. Howev ...

Nested foreach loops interacting with one another

I am attempting to execute multiple nested foreach loops and stop at 12 iterations, however the current code I have is not functioning as expected. While it displays the desired output, there seems to be an issue where only the first image in each director ...

Unable to Toggle Bootstrap 5 Dropdown

Take a look at my code below. <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewpor ...

Adjustable slider height in WordPress

Struggling with the height of the slider on my website. I've tried various CSS adjustments and even tweaked the Jquery, but it still doesn't display properly on mobile devices. For instance, setting a height of 300px looks perfect on desktop brow ...

Address Book on Rails

Hello, I'm relatively new to this and would be grateful for any assistance. My goal is to utilize the data saved by a user in their address book, and then offer them the option to use that address for delivery. Below is my Address controller: class A ...

Error: The function 'check' is not defined and cannot be called when the 'on

My aim is to display the "expandrepeat" div when a specific select option is chosen. Unfortunately, the code below doesn't seem to be working as intended: <script> window.check=function(elem) { if (elem.selectedIndex == 1) { documen ...

Implement a captivating hover effect on every single element within a particular class

I created a basic website with the following design: However, there is an issue with the hover effect. It only works on the specific area where the mouse pointer is located (here ipsum). My intention was for the hover effect to work on all elements. To fi ...

Send a triggering function to two separate components

My objective is as follows: render() { return ( <div> <Child onTrigger={xxx} /> <button onClick={xxx} /> </div> ) } Upon clicking the button, I wish for some action to take place in ...

What could be the reason that Ng Repeat fails to work when a button is triggered from a separate form

I have an HTML table that includes an ng repeat directive and two buttons. The first button opens a modal with a form to create a new user. When I click save, it adds the new user to the list. The second button is within the same form and also adds a user. ...

Access the document on the desktop and open it in a new popup window

As a novice in html coding, I'm wondering if it's possible to write window size and other properties directly into the page. Let me explain. I'm currently working on a calculator and I want to run the HTML file on my desktop. Everything wor ...

Struggling to update the state of an array using ReactJS and unsure of how to iterate through it

Currently, I am in the process of developing a movie voting application for me and my friends using React. I have made significant progress but I am facing issues with setting or updating the state of the 'rating' object. The structure of the ar ...

Entering in full screen mode

Is there a way to create a first screen appearance that spans the entire screen? I want the whole page to be fullscreen and when someone scrolls down, it transitions to a new screen. I am struggling to find a solution for this. body { margin: 0 0 0 0; ...

What steps do you need to take in order to transform this individual slideshow script into numerous slideshows

I came across this code online that effectively creates a slideshow. https://css-tricks.com/snippets/jquery/simple-auto-playing-slideshow/ Is there a way to modify the code to support multiple slideshows? I've made several attempts without success ...

jinja2.exceptions.UndefinedError: The variable 'participant' has not been defined

I am currently in the process of developing a video chat web application using Twilio, and I have been following a tutorial on how to build the application: . However, I keep encountering an error mentioned in the title. It seems like I am trying to access ...

After logging out, Next-auth redirects me straight back to the dashboard

In my NextJS application, I've implemented a credential-based authentication flow along with a dashboard page. To handle cases where an unauthorized user lands on the dashboard route, I've created a custom AccessDenied component. In the getServer ...

Store the response data in a global variable or forward it to another function in Node.js using Express JS

I'm currently working on a function that makes a post request to an API (app.post), sending a token and a URL for redirection to webpay. The challenge I'm facing is saving that token in a global variable so that it can be accessed by another func ...

Display a fixed three levels of highchart Sunburst upon each click in Angular8

Looking to create a dynamic sunburst highchart that displays three levels at a time while allowing interactive drilling. For instance, if there are 5 levels, the chart should initially show the first three levels. When clicking on level 3, levels 2, 3, and ...

Using htaccess to restrict access to a specific URL path based on IP Address

(I don't believe the previous link addresses this question; please refer to the comment below) I am currently utilizing a CMS platform that utilizes a database instead of the usual file-directory structure for managing webpages. Due to the absence of ...

Angular: displaying dates in a specific format while disregarding time zones

Is there a way to format date-time in Angular using DatePipe.format() without converting timezones, regardless of location? For instance, for various examples worldwide (ignoring time differences) I would like to obtain 07/06/2022: console.log('2022-0 ...