Click here to Skip to main content
15,887,676 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to create a button under all comments in my blog to copy to clipboard the current comment that the visitor wants.

I have manage so far to place the button in every comment and to copy to clipboard any id is already determined in the code of the page.

The problem is that the ids of the comments are not determined in the code of the page but they determined pushups every time you load the page by a blogger expresion on the fly. (see the code below)

However if i examine the Page Source with the browser tools, and pick the id of a specific comment and place it in the code below, the code works fine, but what i want is to do that automatically the page it self for every comment the visitor choses... if I'm not clear please tell me to explain further.

Can you lead me out of this fallacy i'm in ?

Thanks

What I have tried:

HTML
/*this is the link that all comments display at the end*/
<a onclick='CopyToClipboard(/*copying to clipboard any element determined by id i put in here*/)'>Copy comment</a>

/*this is the function it calls on click*/
<script>
//<![CDATA[

 function CopyToClipboard(containerid) {
 
    if (window.getSelection) {
        if (window.getSelection().empty) { // Chrome
            window.getSelection().empty();
        } else if (window.getSelection().removeAllRanges) { // Firefox
            window.getSelection().removeAllRanges();
        }
    } else if (document.selection) { // IE?
        document.selection.empty();
    }

    if (document.selection) {
        var range = document.body.createTextRange();
        range.moveToElementText(document.getElementById(containerid));
        range.select().createTextRange();
        document.execCommand("copy");
    } else if (window.getSelection) {
        var range = document.createRange();
        range.selectNode(document.getElementById(containerid));
        window.getSelection().addRange(range);
        document.execCommand("copy");
    }
}
//]]> 
</script>
</body>




/*and this is how the id of any comment is determined, this is a default blogger thing*/
<dd class='comment-body' expr:id='data:widget.instanceId + data:comment.cmtBodyIdPostfix'>
Posted
Updated 10-Nov-19 0:25am
v5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900