# Copy \<td\> text from table in Ui Template on click

**URL:** <https://discourse.nodered.org/t/copy-td-text-from-table-in-ui-template-on-click/95890>\
**Category:** Dashboard\
**Created:** [9 March 2025 21:33 UTC](https://discourse.nodered.org/t/copy-td-text-from-table-in-ui-template-on-click/95890 "2025-03-09T21:33:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mick](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mick/32/80990_2.png) [@Mick](https://discourse.nodered.org/u/Mick)\
**Post date:** [9 March 2025 21:33 UTC](https://discourse.nodered.org/t/copy-td-text-from-table-in-ui-template-on-click/95890/1 "2025-03-09T21:33:00Z")

</div>

I am trying to get my code to copy a value from text and am using Dashboard V1. When I run the flow I get the following error in the console:

> 'Uncaught TypeError: Cannot read properties of undefined (reading 'writeText')

Since V1 dashboard uses Angular I am using scope watch to get the data. The function works and displays 'copied' as it should in the ui but the console message indicates I am not getting the value passed to the function correctly.

I don't think I am correctly passing the data to the textToCopy var

```auto
  scope.$watch('msg', function(msg) {
    $(document).ready(function() {
      $('td').click(function() {
        if(this.id.startsWith("id")){
          var textToCopy = $(this).text();
          console.log(textToCopy);
        $(this).css("background", "lightblue").text("COPIED");
          setTimeout(()=> {
        $(this).css("background", "white").text(textToCopy);
          },500); navigator.clipboard.writeText(textToCopy);
        }
      });
    });
  });
 })(scope);

```

I created a quick flow with only 1 Table TD for testing:

```auto
[{"id":"9127372381ecb1e8","type":"debug","z":"7432ea42210264ed","name":"debug 1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":300,"y":660,"wires":[]},{"id":"51d33c22732a0c54","type":"ui_template","z":"7432ea42210264ed","group":"fa136d15de3a3532","name":"Test Table","order":5,"width":"22","height":"10","format":"<style>\n .text-font {\n font-family: sans-serif;\n // text-align: center; \n }\n\n .styled-table {\n border-collapse: collapse;\n margin: .5em;\n font-size: 0.9em;\n font-family: sans-serif;\n box-shadow: 0 0 20px rgba(0, 0, 0, 0.15);\n }\n\n .styled-table thead tr {\n background-color: #009879;\n color: #ffffff;\n text-align: left;\n }\n\n .styled-table th,\n .styled-table td {\n padding: 5px 10px;\n max-width: .5em;\n text-align: center;\n text-wrap: pretty;\n word-break: break-word;\n }\n\n .styled-table tbody tr {\n border-bottom: 1px solid #dddddd;\n }\n\n .styled-table tbody tr:nth-of-type(even) {\n background-color: #f3f3f3;\n }\n\n .styled-table tbody tr:last-of-type {\n border-bottom: 2px solid #009879;\n }\n</style>\n\n<script>\n \n(function(scope) { \n scope.$watch('msg.payload', function(obj) {\n var globalCounter = 0;\n var tbody = document.getElementById('tbody');\n $(\"tbody#tbody tr\").remove()\n for (var i = 0; i < Object.keys(obj).length; i++) { \n var tr=\"<tr>\" ;\n tr +=\"<td id='id-row-\"+ obj[i].id +\"'\" + \">\" + obj[i].id + \"</td>\" + \"</tr>\" ; \n tbody.innerHTML +=tr; } \n }); \n })(scope); \n\n (function(scope) {\n\n scope.$watch('msg', function(msg) {\n $(document).ready(function() {\n $('td').click(function() {\n if(this.id.startsWith(\"id\")){\n var textToCopy = $(this).text();\n console.log(textToCopy);\n $(this).css(\"background\", \"lightblue\").text(\"COPIED\");\n setTimeout(()=> {\n $(this).css(\"background\", \"white\").text(textToCopy);\n },500); navigator.clipboard.writeText(textToCopy);\n }\n });\n });\n });\n })(scope);\n\n </script>\n\n<table class=\"styled-table\">\n <thead>\n <tr>\n <th>Id </th>\n </tr>\n </thead>\n <tbody id=\"tbody\"></tbody>\n</table>","storeOutMessages":false,"fwdInMessages":false,"resendOnRefresh":false,"templateScope":"local","className":"","x":460,"y":620,"wires":[[]]},{"id":"2f5bc6921a72af80","type":"template","z":"7432ea42210264ed","name":"","field":"payload","fieldType":"msg","format":"json","syntax":"plain","template":"[\n {\n \"id\": 1\n },\n {\n \"id\": 2\n },\n {\n \"id\": 3\n },\n {\n \"id\": 4\n },\n {\n \"id\": 5 \n }\n]","output":"json","x":300,"y":620,"wires":[["51d33c22732a0c54","9127372381ecb1e8"]]},{"id":"9c57e6716794f5d0","type":"inject","z":"7432ea42210264ed","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":160,"y":620,"wires":[["2f5bc6921a72af80"]]},{"id":"fa136d15de3a3532","type":"ui_group","name":"Password","tab":"7c74ed0670b283cd","order":1,"disp":false,"width":"22","collapse":false,"className":""},{"id":"7c74ed0670b283cd","type":"ui_tab","name":"Password List","icon":"dashboard","order":2,"disabled":false,"hidden":false}]

```

Thank you for any advice...

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [9 March 2025 21:49 UTC](https://discourse.nodered.org/t/copy-td-text-from-table-in-ui-template-on-click/95890/2 "2025-03-09T21:49:09Z")

</div>

> [@Mick](#):
>
> 'Uncaught TypeError: Cannot read properties of undefined (reading 'writeText')

and

> [@Mick](#):
>
> `navigator.clipboard.writeText(textToCopy);`

implies that `navigator.clipboard` is undefined

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [9 March 2025 21:52 UTC](https://discourse.nodered.org/t/copy-td-text-from-table-in-ui-template-on-click/95890/3 "2025-03-09T21:52:28Z")

</div>

navigator.clipboard requires a secure origin

I.e. you need to either be using HTTPS or localhost.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [23 March 2025 21:53 UTC](https://discourse.nodered.org/t/copy-td-text-from-table-in-ui-template-on-click/95890/4 "2025-03-23T21:53:20Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
