Having trouble with VueJS? Make sure to give that button two clicks to get the Copy to Clipboard feature working

My current issue involves a text input and copy button located within an accordion section.

https://i.sstatic.net/0JS9W.png

The code for the copy button is as follows:

<accordian>
    <accordian-panel heading="Feed ID">
        <div class="form-row form-row-layout">
              <input type="text" v-model="feed.app_id" id="feedAppId" />
               <button class="btn btn-sm btn-primary" id="copyFeedIdBtn" @click.prevent="copyFeedId()" data-clipboard-target="#feedAppId">Copy</button>
         </div>
    </accordian-panel>
</accordian>

This is the JavaScript method associated with the copy functionality:

copyFeedId()
{
     this.clipboard = new Clipboard(jQuery('#copyFeedIdBtn')[0],{
        container: this.$el
     });
                
      this.clipboard.on('success', (e) => {
             this.$appToasts.success('Feed ID copied to clipboard');
             e.clearSelection();
      });

      this.clipboard.on('error', (e) => {
            console.error('Action:', e.action);
            console.error('Trigger:', e.trigger);
      });
 }

I have observed that I need to click the button twice for it to function properly.

The issue may be related to the fact that the button event is not registered when the accordion section is closed. When I attempted to select jQuery('#copyFeedIdBtn')[0] while the accordion was closed, it returned undefined. I am unsure how to handle this situation and ensure the event is registered even when the accordion is closed. Your assistance in resolving this matter would be greatly appreciated. Thank you!

Answer №1

Have you attempted this method before?

      copyFeedId()
      {
        const element = getElementById('feedAppId');
        element.focus();
        element.setSelectionRange(0, element.value.length);
        try
        {
          const output = document.execCommand('copy');
          if (output)
          {
            // success;
          }
          else
          {
            // failure
            console.log('Could not successfully copy to clipboard');
          }
        }
        catch (error)
        {
          // error
          console.log(error.message || error);
        }
      }

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

Encountering an issue with the vue webpack-simple template: Uncaught TypeError - the intermediate value is not recognized as a function

Encountering a peculiar error while trying to create an IIFE method in the main.js file. Follow these steps to reproduce the issue, open the command prompt vue init webpack-simple test cd test npm install test npm run dev Open the main.js file and insert ...

Leverage JavaScript or CSS to create a paper button without the ink effect

Currently, I am utilizing JavaScript to dynamically create paper-buttons in the following manner: function createButtons(dieDefaults) { for(var i = 0; i < dieDefaults.length; i++) { var btn = document.createElement("paper-button"); var txt = d ...

Is it recommended to implement client-side fetching upon component mounting following the utilization of getServerSideProps?

After implementing getServerSideProps in Next.js, I'm beginning to question the necessity of client-side fetching. If the data is already pre-rendered, is it redundant to fetch it again on component render? I'm unsure about the best approach for ...

Implement a feature on React using hooks that detects when a user clicks

I'm attempting to utilize React hooks to check if a user has clicked outside of an element. I'm using useRef to grab a reference to the element. Could someone help me troubleshoot this? I'm encountering the following errors and referencing ...

Positioning CSS for a Responsive Button

Currently, I am working on making my modal responsive. However, I am encountering an issue with the positioning of the "SAVE" button. The button is not staying in the desired position but instead disappears. Below is the snippet of my HTML and CSS: .dele ...

What is the best way to trigger a re-render of a Vue component after a change in Vuex

I'm interested in developing a custom component to modify the basemap of a leaflet map without relying on L.control.layers. To achieve this, I created a sidebar component that offers options to switch between different basemaps. In my implementation, ...

Sending data to an Express server in Node.js using JavaScript

My current project involves the use of javascript, html, nodejs, express, and mysql to fetch values from a database and send them back to the html interface. In this setup, the user inputs a domain in the 'btnSearch' textbox and clicks on the Lo ...

Using Jquery val() will consistently retrieve the value of the first selected option, regardless of which one is

Utilizing Jquery to enable users to upload images via Ajax in a form, with the image uploading before form submission. A watermark feature has been incorporated into each uploaded image (handled by upload_image.php) and this aspect is functioning correctly ...

Adding dynamic text to a <span> tag within a <p> element is causing a disruption in my layout

I'm encountering an issue with a Dialog box that displays a message to the user regarding file deletion. Here's how it looks: +-----------------------------------------------------------------------+ | Delete File? ...

Will synchronous programming on an Express server prevent other users from accessing the page simultaneously?

Currently working on a simple one-page web app that depends on loading weather data from a file. To avoid multiple HTTP requests for each visitor, I have a separate script refreshing the data periodically. In this particular instance, I am using fs.readFi ...

What is the method for altering the track color on a range slider?

Struggling to change the track color of a range slider using CSS in Chrome? Look no further, as I found a solution using jQuery (link). However, after implementing the solution, the expected output is not achieved. jQuery: $('.text-size-slider .slid ...

What causes me to receive an array JSON response every time I make a call to a method in the Web API

When I call a method in Postman, I receive an array that includes my JSON data as shown below: [ { "spark_version": "7.6.x-scala2.12" } ] The API Method [HttpGet] public IActionResult GetTest(int ActivityId) { string Store ...

How to include an image in a CSS file for a React project

I'm attempting to use an image from public/assets/subtract.png, but I am encountering a resolution issue. Here is the code snippet in question: body { background-image: url("./assets/subtract.png"); } The specific problem I am facing is ...

Updating state: Removing a specific individual from an array of objects when a button is clicked

I am currently working on a code snippet where I aim to remove a specific person from an organizational chart once the delete button next to their name is clicked. However, I am encountering an issue where clicking any delete button results in all 5 people ...

Issue: The property 'top' cannot be read as it is null

I'm trying to access the top of my footer, but I keep encountering this error message: Cannot read property 'top' of null Here is the HTML code: <footer class="footer" role="complementary" id="myfooter"> </footer> And in jQuer ...

Tips for obtaining Karma coverage for handling if-else scenarios in a controller method with Mocha/Jasmine testing suite

Below is the controller method in question: function updateItem(itemList) { vm.selectedItem = ''; if(itemList !== null || itemList !== '' && itemList !== undefined){ if (itemList.firstName !== ...

What steps can I take to enhance security in a Vuejs SPA's Front-End?

For my Vuejs-powered SPA that communicates with Laravel API's, I am faced with the challenge of securely saving user tokens and roles. How can this information be saved without compromising security or revealing role details to unauthorized users? On ...

Uploading Files in HTML

My goal is to create a way to input a file using the following method: Input file: <input type="file" name="-f" id="fa"> I would like to include an HTML link (upload example) that, when clicked, will upload an example file without opening the file ...

What could be causing my fetch() function to send a JSON body that is empty?

I've been struggling with sending JSON data using fetch as the backend keeps receiving an empty object. In my Client JS code, I have the following: const user = "company1"; const username = "muneeb"; const data = {user, username}; fetch("http://127. ...

Add a border to the navigation bar item to indicate the current page being displayed

This section shows the current navigation bar item https://i.sstatic.net/6RAGJ.png This is the desired outcome when clicking on the tab https://i.sstatic.net/8nFeB.png Uncertain about the best approach for achieving this. I have attempted to submit a val ...