0

I work on Angular 4. My page has many checkbox and a share button. If any check box is selected and share is clicked a service is called and the download URL for that checkbox is returned. I have to copy this returned URL so that if user want he can paste it in notepad/browser. How to copy text in a variable inside javascript/typescript/Angular? Please guide.

<div *ngFor="let hero of heros">
    <span>
        <input type='checkbox' name='drama'  />
    </span>
    <span>hero.name</span>
</div>
<button [class.disabled]='!inEditMode' (click)='selectedHero()'>Share</button>

selectedHero(){
    var elements = document.getElementsByName("drama");
    for (let i = 0; i < elements.length; i++) {
        if (elements[i].type == "checkbox") {
            if (elements[i].checked) {
                //Service call is made here and the URL is stored in "this.docURL"
            }
        }
    }
}

How to copy the value inside "this.docURL" ?

Narm
  • 10,677
  • 5
  • 41
  • 54
Anna
  • 1,669
  • 7
  • 38
  • 63
  • according to what you are saying `a service is called and the download URL for that checkbox is returned` you already have a url. You just need to assign. JavaScript is a pass by reference – Lokesh Pandey Jun 06 '18 at 10:42
  • Op wants to copy to cliboard – Guerric P Jun 06 '18 at 10:43
  • @Lokesh, so after I get the download URL if I right click the mouse and click paste that URL should be pasted on the notepad/browser. I am not able to achieve this paste functionality – Anna Jun 06 '18 at 10:46
  • 1
    Did you try https://stackoverflow.com/questions/400212/how-do-i-copy-to-the-clipboard-in-javascript ? (the `Copy to clipboard without displaying input`part) – Guerric P Jun 06 '18 at 10:54

0 Answers0