Focus doesn't remain on button having a script on click event

We are using Pega 7.3 with CRM framework. On a section, we have customer id, name, surame as lables and under each of those, buttons are added with ‘Copy’ caption. On click, a script (JS) is getting executed and the value of label above that button will be copied to clipboard of local system.

Issue is that the focus shifts to some unknown place of the screen and tabbing would show it in the left most tab of the explorer.

What change we need to make in script so that the focus remain on button that we clicked?

Added below function in the script of buttons event and it worked. Here test-id is autogenerated numeric value of button (usually start with yyyy)

<script> 

  setTimeout(function(){ 

  document.querySelectorAll('[data-test-id="xxxxx"]')[0].focus(); 

  }, 100);

</script>

ref post: How to set focus on the Submit button of the form rather than the first action item on form load? | Support Center ​​​​​​​