Steps to generate a duplicate for clipboard with interactive buttons

Hello everyone! I'm currently working on a small project using Django and JavaScript. I am trying to implement a button that allows users to copy information to the clipboard. However, the buttons are dynamic and the site is not using HTTPS, so I cannot use "navigator.clipboard.writeText" at the moment. I have managed to retrieve the information through the specified attributes in my button, but I am stuck on how to actually copy this information to the clipboard. As a newbie, any help or guidance on this would be greatly appreciated :)

Below is the main piece of code:

            {% for item in object_list %}
                <tr>
                    <td class="th-width">{{ item.user.company.name }}</td>
                    <td class="th-width">{{ item.user.user_name }}</td>
                    <td class="th-width">{{ item.user.full_name }}</td>
                    <td class="th-width email-column">{{ item.user.email }}</td>
                    <td class="th-width">{{ item.expire_date }}</td>
                    <td class="th-width">{{ item.avt_version}}</td>
                    <td class="th-width">{{ item.license_avt }}</td>
                    <td class="th-width">{{ item.generated_user.username }}</td>
                    <td class="th-width">
                        <button class="control button copy-btn btn" company-name="{{item.user.company.name}}"  user-name="{{item.user.user_name}}" full-name="{{item.user.full_name}}" email="{{item.user.email}}" expire-date="{{item.expire_date}}">Copy information</button>
                    </td>
                </tr>
            {% endfor %}
        </tbody> 
    </table>
<script>
    const copyBtns = [...document.getElementsByClassName("copy-btn")]
    copyBtns.forEach(btn => btn.addEventListener('click', () => {
        const companyName = btn.getAttribute("company-name")
        const userName = btn.getAttribute("user-name")
        const fullName = btn.getAttribute("full-name")
        const email = btn.getAttribute("email")
        const expireDate = btn.getAttribute("expire-date")

        console.log(companyName)
        console.log(userName)
        console.log(fullName)
        console.log(email)
        console.log(expireDate)
        
        btn.textContent = "Copied";
        setTimeout(() => {
            btn.textContent = "Copy information";
        }, 1350);

    }));
</script>

Answer №1

To enhance your onclick functionality, consider including the following code:

const text = "the specific content you wish to copy";

const newTextArea = document.createElement('textarea');
document.body.appendChild(newTextArea);
newTextArea.value = text;
newTextArea.select();
document.execCommand('copy', false);
newTextArea.remove();

This script snippet generates a temporary textarea element, inserts your desired text, copies it to the clipboard, and then deletes the element.

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

Discover the process of converting the texture in three.js to WebGL

I have received two texture objects containing position and normal data: var tx1 = gpuCompute.getCurrentRenderTarget( positionVariable ).texture; var tx2 = gpuCompute.getCurrentRenderTarget( normalVariable ).texture; These textures are generated using GP ...

Customizable Mongoose schema attribute

Currently, I am in the process of creating a model using Mongoose that will include a field to store the attributes of my object. The challenge I am facing is that these attributes can vary significantly, for example: StockItem1 : { sku: 23492349, ...

When the enter key is pressed, shift the text to a different input field

I am facing a complex requirement How can I detect the pressing of the ENTER key within a text box with the following conditions: If the cursor is at the end of a sentence, it should create a new text box on the next row. If the cursor is in the middle ...

Utilize a search box and dropdown menu to perform real-time searches in the database table

Having an issue with filtering the database, the code below is not displaying the filtered results after clicking the submit button. <form method="POST" action="client.php"> <div id="Search" style="display:none"> <h4>Search Clien ...

When using React Router, make sure to set the navigation tab's style class to "active" if you are

I'm currently working on a React and react-router application that uses 2 nested routes to create a total of 3 routes: /, /about, /contact. When I click on the Links to navigate to each route, the tab on my navigation bar - which is located in the par ...

Implementing Vue's dynamic component addition feature

I recently came across an interesting article on dynamically adding different components in Vue. The article explains a good method for binding different components to tabs, but I have a specific requirement. I want to bind one type/name component that wil ...

Use a try/catch block to handle errors in case the parameter does not exist

Sorting the data array is working fine for me, but now I want to pass values as parameters. However, if the user enters a 'parameter' that doesn't exist, I need to display an error message using TRY/CATCH. Can someone please help me understa ...

Utilizing HTML to emphasize a section of text

Hey there! I have a question related to an image with unicodes. The letter featured in the image was written using unicode characters పా. I'm trying to highlight the white portion of the image, but simply replacing it with ా isn&apos ...

Tips on removing a component when transitioning to a new component within Angular 6

When working with Angular 6, we have three components named x, y, and z. Currently, I am in the x component. However, when navigating to the y component and then returning back to x, the previous instance of x is still present in the DOM. I want to remove ...

Tips for showcasing a drop-down menu using Jquery

I am currently utilizing jQuery to showcase a drop-down menu. It is successfully working for a single menu and displaying the appropriate drop-down. However, when I attempt to use more than one menu, it displays all of the drop-down menus simultaneously. I ...

Utilizing React forwardRef with a functional component

Looking at my code, I have defined an interface as follows: export interface INTERFACE1{ name?: string; label?: string; } Additionally, there is a function component implemented like this: export function FUNCTION1({ name, label }: INTERFACE1) { ...

Unable to utilize JavaScript from the imported AJAX page XMLHttpRequest

I have implemented a bit of AJAX functionality using XMLhttpRequest, where it replaces a div and functions as expected. However, I am facing difficulty in getting the JavaScript code in the included AJAX page to execute. Is there a way to run JavaScript f ...

Troubleshooting: AngularJS fails to refresh UI when using SignalR

When I attempt to incorporate this angularjs code into a signalR function, the UI does not update. Even using $scope.$apply() does not trigger a digest. This is the code snippet: var notificationHub = $.connection.progressNotificationHub; notificationHub. ...

Performing an Ajax request using MooTools when my button is clicked

After clicking a button, I want to initiate an ajax call. There are more than 14 buttons on my site that make ajax requests to fetch elements from various parts of the site. Button, Button1, Button2, Button3 | | | load content | | ...

Sort by characteristics of embedded array - Knockout

I'm struggling with filtering a nested array using Knockout. My goal is to filter the array by both 'tag-names' and 'name'. For example, searching for 'whal' should display all objects that contain a tag with that name ...

Easily move a div by dragging and dropping it from one location to another

Looking to seamlessly transfer a div from one container to another. I've got the CSS and HTML code ready for you to assist me, here is the snippet: <!DOCTYPE html> <html> <head> <style type="text/css"> #my ...

Struggling to implement server side processing with Datatables in MVC4?

Greetings, our current setup involves an MVC4 application that is handling a large number of records with thumbnail images in a jQuery Datatables driven view. Unfortunately, the loading time is slow due to all thumbnails being fetched at once during a GET ...

Transforming Poloniex API Callback JSON into a compatible format for Highcharts.Stockchart

I am currently working on a project that involves retrieving JSON data from Poloniex's public API method (specifically the returnChartData method) to generate a graph using Highchart Stockchart. The graph would display the historical performance of va ...

Error encountered in Django and React when using Axios: Expected 'true' for Access-Control-Allow-Credentials

I have encountered an issue while working on a web app that utilizes React on the front end and Django on the back end. The problem arises when sending API requests to the back end. I am encountering an Axios Network Error message stating: Cross-Origin Req ...

Ensuring Bootstrap retains fullscreen tables while adjusting window size

I have set up a webpage containing a table and a footer. Utilizing bootstrap, I ensured that the footer remains visible while the table adjusts its height based on the remaining space on the screen. While this resizing works perfectly when the window size ...