Adding copy button with function based on cell content












1














I have a table that has some data that when double clicked to select/copy/paste ends up with some white space on either side of the test (could be from some of the other items inside the cell).



My solution is to have a button show the right of the text that copies the text when clicked.(thanks to jon-p, I have the buttons iterated through the cells)



My question is how can I add a function that copies the text of the cell that the new button is in.



Also, I can't edit the page directly so I'm using tampermonkey to inject the code.



http://jsfiddle.net/pshock13/kcvbyq9r/



<table>
<thead>
<th>Tools</th>
<th>Shipment</th>
<th>Barcode</th>
<th>More Info</th>
</thead>
<tbody>
<tr>
<td><span>✔ ✘</span></td>
<td>
<div class="relative">
<a href="something.com/Search?searchKey=123456789">123456789</a>
</div>
</td>
<td>
<div class="relative">
<a href="na.something.com/results?s=asdfghjkl">asdfghjkl</a>
</div>
</td>
<td>
<div class="relative">
<span>9870356542</span>
</div>
</td>
</tr>
<tr>
<td><span>✔ ✘</span></td>
<td>
<div class="relative">
<a href="something.com/Search?searchKey=987654321">987654321</a>
</div>
</td>
<td>
<div class="relative">
<a href="na.something.com/results?s=qwertyuiop">qwertyuiop</a>
</div>
</td>
<td>
<div class="relative">
<span>asfg456sdfg</span>
</div>
</td>
</tr>
<tr>
<td><span>✔ ✘</span></td>
<td>
<div class="relative">
<a href="something.com/Search?searchKey=123456789">123456789</a>
</div>
</td>
<td>
<div class="relative">
<a href="na.something.com/results?s=asdfghjkl">asdfghjkl</a>
</div>
</td>
<td>
<div class="relative">
<span>9870356542</span>
</div>
</td>
</tr>
<tr>
<td><span>✔ ✘</span></td>
<td>
<div class="relative">
<a href="something.com/Search?searchKey=987654321">987654321</a>
</div>
</td>
<td>
<div class="relative">
<a href="na.something.com/results?s=qwertyuiop">qwertyuiop</a>
</div>
</td>
<td>
<div class="relative">
<span>asfg456sdfg</span>
</div>
</td>
</tr>
</tbody>
</table>

var copyBtn = "<span class='copy' onClick='copyText()'>📋</span>"

var shipmentCells = document.querySelectorAll("tbody tr > td:nth-child(2) > div");
for(var i = 0; i < shipmentCells.length; i++){
//Append the new element to the innerHTML
shipmentCells[i].innerHTML += copyBtn;
}









share|improve this question





























    1














    I have a table that has some data that when double clicked to select/copy/paste ends up with some white space on either side of the test (could be from some of the other items inside the cell).



    My solution is to have a button show the right of the text that copies the text when clicked.(thanks to jon-p, I have the buttons iterated through the cells)



    My question is how can I add a function that copies the text of the cell that the new button is in.



    Also, I can't edit the page directly so I'm using tampermonkey to inject the code.



    http://jsfiddle.net/pshock13/kcvbyq9r/



    <table>
    <thead>
    <th>Tools</th>
    <th>Shipment</th>
    <th>Barcode</th>
    <th>More Info</th>
    </thead>
    <tbody>
    <tr>
    <td><span>✔ ✘</span></td>
    <td>
    <div class="relative">
    <a href="something.com/Search?searchKey=123456789">123456789</a>
    </div>
    </td>
    <td>
    <div class="relative">
    <a href="na.something.com/results?s=asdfghjkl">asdfghjkl</a>
    </div>
    </td>
    <td>
    <div class="relative">
    <span>9870356542</span>
    </div>
    </td>
    </tr>
    <tr>
    <td><span>✔ ✘</span></td>
    <td>
    <div class="relative">
    <a href="something.com/Search?searchKey=987654321">987654321</a>
    </div>
    </td>
    <td>
    <div class="relative">
    <a href="na.something.com/results?s=qwertyuiop">qwertyuiop</a>
    </div>
    </td>
    <td>
    <div class="relative">
    <span>asfg456sdfg</span>
    </div>
    </td>
    </tr>
    <tr>
    <td><span>✔ ✘</span></td>
    <td>
    <div class="relative">
    <a href="something.com/Search?searchKey=123456789">123456789</a>
    </div>
    </td>
    <td>
    <div class="relative">
    <a href="na.something.com/results?s=asdfghjkl">asdfghjkl</a>
    </div>
    </td>
    <td>
    <div class="relative">
    <span>9870356542</span>
    </div>
    </td>
    </tr>
    <tr>
    <td><span>✔ ✘</span></td>
    <td>
    <div class="relative">
    <a href="something.com/Search?searchKey=987654321">987654321</a>
    </div>
    </td>
    <td>
    <div class="relative">
    <a href="na.something.com/results?s=qwertyuiop">qwertyuiop</a>
    </div>
    </td>
    <td>
    <div class="relative">
    <span>asfg456sdfg</span>
    </div>
    </td>
    </tr>
    </tbody>
    </table>

    var copyBtn = "<span class='copy' onClick='copyText()'>📋</span>"

    var shipmentCells = document.querySelectorAll("tbody tr > td:nth-child(2) > div");
    for(var i = 0; i < shipmentCells.length; i++){
    //Append the new element to the innerHTML
    shipmentCells[i].innerHTML += copyBtn;
    }









    share|improve this question



























      1












      1








      1







      I have a table that has some data that when double clicked to select/copy/paste ends up with some white space on either side of the test (could be from some of the other items inside the cell).



      My solution is to have a button show the right of the text that copies the text when clicked.(thanks to jon-p, I have the buttons iterated through the cells)



      My question is how can I add a function that copies the text of the cell that the new button is in.



      Also, I can't edit the page directly so I'm using tampermonkey to inject the code.



      http://jsfiddle.net/pshock13/kcvbyq9r/



      <table>
      <thead>
      <th>Tools</th>
      <th>Shipment</th>
      <th>Barcode</th>
      <th>More Info</th>
      </thead>
      <tbody>
      <tr>
      <td><span>✔ ✘</span></td>
      <td>
      <div class="relative">
      <a href="something.com/Search?searchKey=123456789">123456789</a>
      </div>
      </td>
      <td>
      <div class="relative">
      <a href="na.something.com/results?s=asdfghjkl">asdfghjkl</a>
      </div>
      </td>
      <td>
      <div class="relative">
      <span>9870356542</span>
      </div>
      </td>
      </tr>
      <tr>
      <td><span>✔ ✘</span></td>
      <td>
      <div class="relative">
      <a href="something.com/Search?searchKey=987654321">987654321</a>
      </div>
      </td>
      <td>
      <div class="relative">
      <a href="na.something.com/results?s=qwertyuiop">qwertyuiop</a>
      </div>
      </td>
      <td>
      <div class="relative">
      <span>asfg456sdfg</span>
      </div>
      </td>
      </tr>
      <tr>
      <td><span>✔ ✘</span></td>
      <td>
      <div class="relative">
      <a href="something.com/Search?searchKey=123456789">123456789</a>
      </div>
      </td>
      <td>
      <div class="relative">
      <a href="na.something.com/results?s=asdfghjkl">asdfghjkl</a>
      </div>
      </td>
      <td>
      <div class="relative">
      <span>9870356542</span>
      </div>
      </td>
      </tr>
      <tr>
      <td><span>✔ ✘</span></td>
      <td>
      <div class="relative">
      <a href="something.com/Search?searchKey=987654321">987654321</a>
      </div>
      </td>
      <td>
      <div class="relative">
      <a href="na.something.com/results?s=qwertyuiop">qwertyuiop</a>
      </div>
      </td>
      <td>
      <div class="relative">
      <span>asfg456sdfg</span>
      </div>
      </td>
      </tr>
      </tbody>
      </table>

      var copyBtn = "<span class='copy' onClick='copyText()'>📋</span>"

      var shipmentCells = document.querySelectorAll("tbody tr > td:nth-child(2) > div");
      for(var i = 0; i < shipmentCells.length; i++){
      //Append the new element to the innerHTML
      shipmentCells[i].innerHTML += copyBtn;
      }









      share|improve this question















      I have a table that has some data that when double clicked to select/copy/paste ends up with some white space on either side of the test (could be from some of the other items inside the cell).



      My solution is to have a button show the right of the text that copies the text when clicked.(thanks to jon-p, I have the buttons iterated through the cells)



      My question is how can I add a function that copies the text of the cell that the new button is in.



      Also, I can't edit the page directly so I'm using tampermonkey to inject the code.



      http://jsfiddle.net/pshock13/kcvbyq9r/



      <table>
      <thead>
      <th>Tools</th>
      <th>Shipment</th>
      <th>Barcode</th>
      <th>More Info</th>
      </thead>
      <tbody>
      <tr>
      <td><span>✔ ✘</span></td>
      <td>
      <div class="relative">
      <a href="something.com/Search?searchKey=123456789">123456789</a>
      </div>
      </td>
      <td>
      <div class="relative">
      <a href="na.something.com/results?s=asdfghjkl">asdfghjkl</a>
      </div>
      </td>
      <td>
      <div class="relative">
      <span>9870356542</span>
      </div>
      </td>
      </tr>
      <tr>
      <td><span>✔ ✘</span></td>
      <td>
      <div class="relative">
      <a href="something.com/Search?searchKey=987654321">987654321</a>
      </div>
      </td>
      <td>
      <div class="relative">
      <a href="na.something.com/results?s=qwertyuiop">qwertyuiop</a>
      </div>
      </td>
      <td>
      <div class="relative">
      <span>asfg456sdfg</span>
      </div>
      </td>
      </tr>
      <tr>
      <td><span>✔ ✘</span></td>
      <td>
      <div class="relative">
      <a href="something.com/Search?searchKey=123456789">123456789</a>
      </div>
      </td>
      <td>
      <div class="relative">
      <a href="na.something.com/results?s=asdfghjkl">asdfghjkl</a>
      </div>
      </td>
      <td>
      <div class="relative">
      <span>9870356542</span>
      </div>
      </td>
      </tr>
      <tr>
      <td><span>✔ ✘</span></td>
      <td>
      <div class="relative">
      <a href="something.com/Search?searchKey=987654321">987654321</a>
      </div>
      </td>
      <td>
      <div class="relative">
      <a href="na.something.com/results?s=qwertyuiop">qwertyuiop</a>
      </div>
      </td>
      <td>
      <div class="relative">
      <span>asfg456sdfg</span>
      </div>
      </td>
      </tr>
      </tbody>
      </table>

      var copyBtn = "<span class='copy' onClick='copyText()'>📋</span>"

      var shipmentCells = document.querySelectorAll("tbody tr > td:nth-child(2) > div");
      for(var i = 0; i < shipmentCells.length; i++){
      //Append the new element to the innerHTML
      shipmentCells[i].innerHTML += copyBtn;
      }






      javascript copy tampermonkey






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 at 8:19

























      asked Nov 12 at 7:44









      Pshock13

      345




      345
























          1 Answer
          1






          active

          oldest

          votes


















          1














          You can use the GM_setClipboard() function to make this easy.



          Important:





          1. Don't use onclick.


          2. Using .innerHTML is also poor practice -- doubly so in a userscript.

          3. When used with @require, there is almost no downside to using jQuery, and a lot of gain in coding ease, speed, and simplicity.


          Here's a complete working userscript that adds and activates copy buttons. I added some optional formatting and UI, just for giggles:



          // ==UserScript==
          // @name _Add copy buttons to a table
          // @match *://YOUR_SERVER.COM/YOUR_PATH/*
          // @match https://output.jsbin.com/vuyewal
          // @require https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
          // @grant GM_addStyle
          // @grant GM_setClipboard
          // ==/UserScript==
          /* global $ */
          /* eslint-disable no-multi-spaces */

          //-- Add copy button to column 2:
          $("td:nth-child(2) > div.relative").after (`<span class='tmCopyBtn'>📋</span>`);

          //-- Style it:
          GM_addStyle ( `
          .tmCopyBtn { cursor: pointer; }
          /* Also tweak the div style: */
          td:nth-child(2) > div.relative { display: inline-block; margin-right: 1ex;}

          /* Also add blinker effect for better UI: */
          .justCopied { animation: blinkYellow 1s ease-out 2; }
          @keyframes blinkYellow {
          50% { background-color: yellow; }
          }
          ` );

          //-- Activate it:
          $("table").on ("click", ".tmCopyBtn", zEvent => {
          //-- Get text of adjacent <div> and strip leading/trialing whitespace:
          var targetDiv = $(zEvent.target).prev ("div.relative");
          var textToCopy = targetDiv.text ().trim ();

          GM_setClipboard (textToCopy, "text/plain");

          //-- Feedback to user:
          $(".justCopied").removeClass ("justCopied");
          targetDiv.parent ().addClass ("justCopied");
          } );


          Anyone can test it against this target page at JS Bin.






          share|improve this answer























          • Okay, so this works. Sorta. I have a few tools inside the div with the shipID that cover the copy button when they are hovered. To fix this I used .append instead of .after when creating the button, which works fine until you try to copy using the button. Since it is now inside the div with the text. Found solution: .prev ("a"); on that note, I already have jquery 3.3.1 linked for something else in the project, can i leave just that for this to work properly or do I need both?
            – Pshock13
            Nov 13 at 0:28








          • 1




            If you have already @required jQuery 3.3.1, then that's good enough. If you have added it some other way, then you either need both or to remove the jQ 3.3.1. Your script now runs in a sandbox (a requirement and side effect of GM_setClipboard). So it will only see what you @require.
            – Brock Adams
            Nov 13 at 0:34










          • I have it as @required already. And I already had a .css file for other things as I feel it is just easier than using GM_addStyle.
            – Pshock13
            Nov 13 at 0:36










          • That's fine/good for both.
            – Brock Adams
            Nov 13 at 0:38











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53257754%2fadding-copy-button-with-function-based-on-cell-content%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          You can use the GM_setClipboard() function to make this easy.



          Important:





          1. Don't use onclick.


          2. Using .innerHTML is also poor practice -- doubly so in a userscript.

          3. When used with @require, there is almost no downside to using jQuery, and a lot of gain in coding ease, speed, and simplicity.


          Here's a complete working userscript that adds and activates copy buttons. I added some optional formatting and UI, just for giggles:



          // ==UserScript==
          // @name _Add copy buttons to a table
          // @match *://YOUR_SERVER.COM/YOUR_PATH/*
          // @match https://output.jsbin.com/vuyewal
          // @require https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
          // @grant GM_addStyle
          // @grant GM_setClipboard
          // ==/UserScript==
          /* global $ */
          /* eslint-disable no-multi-spaces */

          //-- Add copy button to column 2:
          $("td:nth-child(2) > div.relative").after (`<span class='tmCopyBtn'>📋</span>`);

          //-- Style it:
          GM_addStyle ( `
          .tmCopyBtn { cursor: pointer; }
          /* Also tweak the div style: */
          td:nth-child(2) > div.relative { display: inline-block; margin-right: 1ex;}

          /* Also add blinker effect for better UI: */
          .justCopied { animation: blinkYellow 1s ease-out 2; }
          @keyframes blinkYellow {
          50% { background-color: yellow; }
          }
          ` );

          //-- Activate it:
          $("table").on ("click", ".tmCopyBtn", zEvent => {
          //-- Get text of adjacent <div> and strip leading/trialing whitespace:
          var targetDiv = $(zEvent.target).prev ("div.relative");
          var textToCopy = targetDiv.text ().trim ();

          GM_setClipboard (textToCopy, "text/plain");

          //-- Feedback to user:
          $(".justCopied").removeClass ("justCopied");
          targetDiv.parent ().addClass ("justCopied");
          } );


          Anyone can test it against this target page at JS Bin.






          share|improve this answer























          • Okay, so this works. Sorta. I have a few tools inside the div with the shipID that cover the copy button when they are hovered. To fix this I used .append instead of .after when creating the button, which works fine until you try to copy using the button. Since it is now inside the div with the text. Found solution: .prev ("a"); on that note, I already have jquery 3.3.1 linked for something else in the project, can i leave just that for this to work properly or do I need both?
            – Pshock13
            Nov 13 at 0:28








          • 1




            If you have already @required jQuery 3.3.1, then that's good enough. If you have added it some other way, then you either need both or to remove the jQ 3.3.1. Your script now runs in a sandbox (a requirement and side effect of GM_setClipboard). So it will only see what you @require.
            – Brock Adams
            Nov 13 at 0:34










          • I have it as @required already. And I already had a .css file for other things as I feel it is just easier than using GM_addStyle.
            – Pshock13
            Nov 13 at 0:36










          • That's fine/good for both.
            – Brock Adams
            Nov 13 at 0:38
















          1














          You can use the GM_setClipboard() function to make this easy.



          Important:





          1. Don't use onclick.


          2. Using .innerHTML is also poor practice -- doubly so in a userscript.

          3. When used with @require, there is almost no downside to using jQuery, and a lot of gain in coding ease, speed, and simplicity.


          Here's a complete working userscript that adds and activates copy buttons. I added some optional formatting and UI, just for giggles:



          // ==UserScript==
          // @name _Add copy buttons to a table
          // @match *://YOUR_SERVER.COM/YOUR_PATH/*
          // @match https://output.jsbin.com/vuyewal
          // @require https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
          // @grant GM_addStyle
          // @grant GM_setClipboard
          // ==/UserScript==
          /* global $ */
          /* eslint-disable no-multi-spaces */

          //-- Add copy button to column 2:
          $("td:nth-child(2) > div.relative").after (`<span class='tmCopyBtn'>📋</span>`);

          //-- Style it:
          GM_addStyle ( `
          .tmCopyBtn { cursor: pointer; }
          /* Also tweak the div style: */
          td:nth-child(2) > div.relative { display: inline-block; margin-right: 1ex;}

          /* Also add blinker effect for better UI: */
          .justCopied { animation: blinkYellow 1s ease-out 2; }
          @keyframes blinkYellow {
          50% { background-color: yellow; }
          }
          ` );

          //-- Activate it:
          $("table").on ("click", ".tmCopyBtn", zEvent => {
          //-- Get text of adjacent <div> and strip leading/trialing whitespace:
          var targetDiv = $(zEvent.target).prev ("div.relative");
          var textToCopy = targetDiv.text ().trim ();

          GM_setClipboard (textToCopy, "text/plain");

          //-- Feedback to user:
          $(".justCopied").removeClass ("justCopied");
          targetDiv.parent ().addClass ("justCopied");
          } );


          Anyone can test it against this target page at JS Bin.






          share|improve this answer























          • Okay, so this works. Sorta. I have a few tools inside the div with the shipID that cover the copy button when they are hovered. To fix this I used .append instead of .after when creating the button, which works fine until you try to copy using the button. Since it is now inside the div with the text. Found solution: .prev ("a"); on that note, I already have jquery 3.3.1 linked for something else in the project, can i leave just that for this to work properly or do I need both?
            – Pshock13
            Nov 13 at 0:28








          • 1




            If you have already @required jQuery 3.3.1, then that's good enough. If you have added it some other way, then you either need both or to remove the jQ 3.3.1. Your script now runs in a sandbox (a requirement and side effect of GM_setClipboard). So it will only see what you @require.
            – Brock Adams
            Nov 13 at 0:34










          • I have it as @required already. And I already had a .css file for other things as I feel it is just easier than using GM_addStyle.
            – Pshock13
            Nov 13 at 0:36










          • That's fine/good for both.
            – Brock Adams
            Nov 13 at 0:38














          1












          1








          1






          You can use the GM_setClipboard() function to make this easy.



          Important:





          1. Don't use onclick.


          2. Using .innerHTML is also poor practice -- doubly so in a userscript.

          3. When used with @require, there is almost no downside to using jQuery, and a lot of gain in coding ease, speed, and simplicity.


          Here's a complete working userscript that adds and activates copy buttons. I added some optional formatting and UI, just for giggles:



          // ==UserScript==
          // @name _Add copy buttons to a table
          // @match *://YOUR_SERVER.COM/YOUR_PATH/*
          // @match https://output.jsbin.com/vuyewal
          // @require https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
          // @grant GM_addStyle
          // @grant GM_setClipboard
          // ==/UserScript==
          /* global $ */
          /* eslint-disable no-multi-spaces */

          //-- Add copy button to column 2:
          $("td:nth-child(2) > div.relative").after (`<span class='tmCopyBtn'>📋</span>`);

          //-- Style it:
          GM_addStyle ( `
          .tmCopyBtn { cursor: pointer; }
          /* Also tweak the div style: */
          td:nth-child(2) > div.relative { display: inline-block; margin-right: 1ex;}

          /* Also add blinker effect for better UI: */
          .justCopied { animation: blinkYellow 1s ease-out 2; }
          @keyframes blinkYellow {
          50% { background-color: yellow; }
          }
          ` );

          //-- Activate it:
          $("table").on ("click", ".tmCopyBtn", zEvent => {
          //-- Get text of adjacent <div> and strip leading/trialing whitespace:
          var targetDiv = $(zEvent.target).prev ("div.relative");
          var textToCopy = targetDiv.text ().trim ();

          GM_setClipboard (textToCopy, "text/plain");

          //-- Feedback to user:
          $(".justCopied").removeClass ("justCopied");
          targetDiv.parent ().addClass ("justCopied");
          } );


          Anyone can test it against this target page at JS Bin.






          share|improve this answer














          You can use the GM_setClipboard() function to make this easy.



          Important:





          1. Don't use onclick.


          2. Using .innerHTML is also poor practice -- doubly so in a userscript.

          3. When used with @require, there is almost no downside to using jQuery, and a lot of gain in coding ease, speed, and simplicity.


          Here's a complete working userscript that adds and activates copy buttons. I added some optional formatting and UI, just for giggles:



          // ==UserScript==
          // @name _Add copy buttons to a table
          // @match *://YOUR_SERVER.COM/YOUR_PATH/*
          // @match https://output.jsbin.com/vuyewal
          // @require https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
          // @grant GM_addStyle
          // @grant GM_setClipboard
          // ==/UserScript==
          /* global $ */
          /* eslint-disable no-multi-spaces */

          //-- Add copy button to column 2:
          $("td:nth-child(2) > div.relative").after (`<span class='tmCopyBtn'>📋</span>`);

          //-- Style it:
          GM_addStyle ( `
          .tmCopyBtn { cursor: pointer; }
          /* Also tweak the div style: */
          td:nth-child(2) > div.relative { display: inline-block; margin-right: 1ex;}

          /* Also add blinker effect for better UI: */
          .justCopied { animation: blinkYellow 1s ease-out 2; }
          @keyframes blinkYellow {
          50% { background-color: yellow; }
          }
          ` );

          //-- Activate it:
          $("table").on ("click", ".tmCopyBtn", zEvent => {
          //-- Get text of adjacent <div> and strip leading/trialing whitespace:
          var targetDiv = $(zEvent.target).prev ("div.relative");
          var textToCopy = targetDiv.text ().trim ();

          GM_setClipboard (textToCopy, "text/plain");

          //-- Feedback to user:
          $(".justCopied").removeClass ("justCopied");
          targetDiv.parent ().addClass ("justCopied");
          } );


          Anyone can test it against this target page at JS Bin.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 13 at 0:36

























          answered Nov 12 at 22:10









          Brock Adams

          68.1k14155213




          68.1k14155213












          • Okay, so this works. Sorta. I have a few tools inside the div with the shipID that cover the copy button when they are hovered. To fix this I used .append instead of .after when creating the button, which works fine until you try to copy using the button. Since it is now inside the div with the text. Found solution: .prev ("a"); on that note, I already have jquery 3.3.1 linked for something else in the project, can i leave just that for this to work properly or do I need both?
            – Pshock13
            Nov 13 at 0:28








          • 1




            If you have already @required jQuery 3.3.1, then that's good enough. If you have added it some other way, then you either need both or to remove the jQ 3.3.1. Your script now runs in a sandbox (a requirement and side effect of GM_setClipboard). So it will only see what you @require.
            – Brock Adams
            Nov 13 at 0:34










          • I have it as @required already. And I already had a .css file for other things as I feel it is just easier than using GM_addStyle.
            – Pshock13
            Nov 13 at 0:36










          • That's fine/good for both.
            – Brock Adams
            Nov 13 at 0:38


















          • Okay, so this works. Sorta. I have a few tools inside the div with the shipID that cover the copy button when they are hovered. To fix this I used .append instead of .after when creating the button, which works fine until you try to copy using the button. Since it is now inside the div with the text. Found solution: .prev ("a"); on that note, I already have jquery 3.3.1 linked for something else in the project, can i leave just that for this to work properly or do I need both?
            – Pshock13
            Nov 13 at 0:28








          • 1




            If you have already @required jQuery 3.3.1, then that's good enough. If you have added it some other way, then you either need both or to remove the jQ 3.3.1. Your script now runs in a sandbox (a requirement and side effect of GM_setClipboard). So it will only see what you @require.
            – Brock Adams
            Nov 13 at 0:34










          • I have it as @required already. And I already had a .css file for other things as I feel it is just easier than using GM_addStyle.
            – Pshock13
            Nov 13 at 0:36










          • That's fine/good for both.
            – Brock Adams
            Nov 13 at 0:38
















          Okay, so this works. Sorta. I have a few tools inside the div with the shipID that cover the copy button when they are hovered. To fix this I used .append instead of .after when creating the button, which works fine until you try to copy using the button. Since it is now inside the div with the text. Found solution: .prev ("a"); on that note, I already have jquery 3.3.1 linked for something else in the project, can i leave just that for this to work properly or do I need both?
          – Pshock13
          Nov 13 at 0:28






          Okay, so this works. Sorta. I have a few tools inside the div with the shipID that cover the copy button when they are hovered. To fix this I used .append instead of .after when creating the button, which works fine until you try to copy using the button. Since it is now inside the div with the text. Found solution: .prev ("a"); on that note, I already have jquery 3.3.1 linked for something else in the project, can i leave just that for this to work properly or do I need both?
          – Pshock13
          Nov 13 at 0:28






          1




          1




          If you have already @required jQuery 3.3.1, then that's good enough. If you have added it some other way, then you either need both or to remove the jQ 3.3.1. Your script now runs in a sandbox (a requirement and side effect of GM_setClipboard). So it will only see what you @require.
          – Brock Adams
          Nov 13 at 0:34




          If you have already @required jQuery 3.3.1, then that's good enough. If you have added it some other way, then you either need both or to remove the jQ 3.3.1. Your script now runs in a sandbox (a requirement and side effect of GM_setClipboard). So it will only see what you @require.
          – Brock Adams
          Nov 13 at 0:34












          I have it as @required already. And I already had a .css file for other things as I feel it is just easier than using GM_addStyle.
          – Pshock13
          Nov 13 at 0:36




          I have it as @required already. And I already had a .css file for other things as I feel it is just easier than using GM_addStyle.
          – Pshock13
          Nov 13 at 0:36












          That's fine/good for both.
          – Brock Adams
          Nov 13 at 0:38




          That's fine/good for both.
          – Brock Adams
          Nov 13 at 0:38


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53257754%2fadding-copy-button-with-function-based-on-cell-content%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Florida Star v. B. J. F.

          Danny Elfman

          Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues