Forum Discussion

JordanMcAlister's avatar
JordanMcAlister
Qrew Captain
6 years ago

Copy Button Code Help

Hi,

I am making a copy button using one of Dan's pasties to copy text directly from a field because highlighting it and copying doesn't seem to keep any formatting. The field is a Rich text formula field and has a lot of html to edit font, background color, padding, etc. I can't seem to get my button to work properly, and I think it has to do with the API_DoQuery part because i have substituted just straight text into my navigator.clipboard.writeText(mytext) and it works fine but when its trying to get info from a field, it pastes nothing.

Can anyone help?

*****

The fid of the field I'm trying to copy from is 165 and is called "STB:Sketch Text"

The record id field has its name changed to [Pole ID#]

Here is my "copybutton.js" page code:
_______________________________________________________________________
(function() {

  var dbidTable1 = "bmwnwyvin";
  var apptoken = "myapptoken";
  $.ajaxSetup({data: {apptoken: apptoken}});

  var querystring = document.location.search;

  if (/a=dr/i.test(querystring)) {
    $("img[qbu=module]").closest("td").css("background-color", "#FFFFFF");
    var audio = new Audio("http://dl.dropboxusercontent.com/s/7ubb14hilzkgnfk/slipkid.mp3");

    $("#QBU_Clipboard").on("click", event => {
      audio.play();
      var rid = QBU_Clipboard.dataset.rid;

      $.get(dbidTable1, {
        act: "API_DoQuery",
        query: '{3.EX.${rid}}',
        clist: "165"
      }).then(function(xml) {

        var mytext = $("STB:Sketch Text", xml).text();
       

        navigator.clipboard.writeText(mytext)
          .then(() => {
            $.jGrowl("Text Copied", {life: 5000, theme: 'jGrowl-green'});
          });
      });

    });
  }

})();
________________________________________________________________________



Here is my "STB:Copy Button" field code:
________________________________________________________________________
[iol] & "copybutton.js" & [/iol]
&
"<a id='QBU_Clipboard' class='Vibrant Success' " &
"  data-rid='" & [Pole ID#] & "'" &
">Copy To Clipboard</a>"
________________________________________________________________________
  • It looks like you forgot to add the DBID for your first variable definition. From Dan's pastie:
    • JordanMcAlister's avatar
      JordanMcAlister
      Qrew Captain
      I initially had it in there, but I couldn't find where the variable was being used in the code
  • For what it's worth (which isn't much), I did try to get this to work on an app of mine, with a single field, and didn't have any luck. I got the growl notification and the audio played, but nothing seemed to actually copy to the clipboard...
    • JordanMcAlister's avatar
      JordanMcAlister
      Qrew Captain
      yep, exactly what's happening to me at the moment; I've also tried to "Inspect" Dan's "Copy to Clipboard" button on his Pasties pages and am trying to figure out the code for that, but reading code is a little outside my expertise. His button there seems to work on a single field, but the code looks a little different and seems to either call to a code page or be straight from a rich text formula field. Everything I seem to try doesn't work.