I have a jQuery which runs through all my divs with a "code" class and prepends a button to them. The button is supposed to call another function to copy the content of that div to the clipboard. However it seems that Sharepoint does not like elements calling javascripts, so while the buttons are successfully created, I am unable to call any other script through them.

I found a related query which deals with Sharepoint striping the event handler from the HTML and the answer recommends adding the button through the Content Editor, however that seems to be hardly an option if the buttons are generated through a script.

sharepoint 2013 button onclick event gets removed after page save

function GenerateButtons() {
  $(".code").each(function(i) {
    var currentNode = $(this);  
    currentNode.attr("id", "div" + i);
    currentNode.prepend("<button type='button' class='right' onclick='CopyToClipboard(" + i + ")'>Copy Code</button><br/>");    

function CopyToClipboard(element) {

Edit: So it turns out that I was mistaken, the event handler is not stripped, it is the function itself. When run from console, the function returns "undefined" even if I remove the argument and replace the one in console log by a literal:

function CopyToClipboard() {  
  console.log("Hello, world");

Edit2: Not sure how important that is, but when the function is called from console, it returns "undefined", but when I press the button itself, there is no output in console at all.

  • Try calling the function in below way: CopyToClipboard(\' '+ i +'\') Commented May 6, 2019 at 11:57
  • @Sohail This does not appear to be a problem with the original function and passing the argument but rather with calling the function in general. See my last update.
    – Eleshar
    Commented May 6, 2019 at 15:12
  • How are you adding your Javascript code to the page? Commented May 6, 2019 at 15:26
  • $(document).on('click', '#youbuttonID', function() { console.log('hi there') }); Try that
    – Marco
    Commented May 6, 2019 at 15:41
  • 1
    So it seems I have found a "solution". I changed the name of the function and it works... Is it possible that it is already built in or a keyword?
    – Eleshar
    Commented May 6, 2019 at 20:16

1 Answer 1


You could try using jQuery to attach the click handler instead of doing it inline with the onclick attribute, like:

var button = currentNode.prepend("<button type='button' class='right'>Copy Code</button>");
// add that extra br

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.