1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

JS copy textarea button

Discussion in 'JavaScript' started by Javver, Feb 14, 2021.

  1. #1
    Hi guys, i'm a grade 9 student and struggling with my small JS project. my teacher wants me to create a password generator. he wants me to create a Copy button that copies the random password that will display the word "copied" and display "field is empty" when blank. so far this is what i have

    
    </style>
    
    </head>
    <script language="javascript" type="text/javascript">
    function randomString() {
        var chars = "00112233445566778899AaBbCcDdEdFfGgHhIiJjKkLLMmNnOoPpQqRrSsTtUuVvWwXxYyZz";
        var string_length = 8;
        var randomstring = '';
        for (var i=0; i<string_length; i++) {
            var rnum = Math.floor(Math.random() * chars.length);
            randomstring += chars.substring(rnum,rnum+1);
        }
        document.randform.randomfield.value = randomstring;
    }
    
    // Start copy button here
    
    function copy() {
      let textarea = document.getElementById("myInput");
      textarea.select();
      document.execCommand("copy");
    }
    
    // END copy button
    
    </script>
    
    
    <form name="randform" align="center">
    <input type="button"
        style="font-family:Consolas;color:#FFFFFF"
        class="button button1"
        value="Generate"
        onClick="randomString();">&nbsp;
    <input type="text"
        id="myInput"
        name="randomfield"
        value=""
        style="color:blue; padding: 12px 16px; font-size:20px; font-family:Consolas; font-weight:bold; background:#FCF4A3; width: 10%;">&nbsp
    
    <input type="button"
        style="font-family:Consolas;color:#FFFFFF"
        class="button copybtn"
        value="COPY"
        onClick="copy('myInput');">&nbsp;
    
    
    </form>
    
    HTML:

     
    Last edited by a moderator: Feb 14, 2021
    Javver, Feb 14, 2021 IP
  2. Efetobor Agbontaen

    Efetobor Agbontaen Active Member

    Messages:
    136
    Likes Received:
    41
    Best Answers:
    5
    Trophy Points:
    85
    #2
    Here's a snippet you could use:

    function copyInput(id) {
    //Get input field reference
    var inputField = document.getElementById(id);
    
    //Select the text
    inputField.select();
    
    //Execute the copy command
    document.execCommand("copy");
    
    //BOOM
    alert(inputField.value + "copied to Clipboard");
    
    Code (JavaScript):
    Then call the method like:

    
    onClick="copyInput('myInput');">
    
    Code (JavaScript):
     
    Efetobor Agbontaen, Feb 14, 2021 IP
  3. Javver

    Javver Active Member

    Messages:
    72
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    91
    #3
    Thanks for looking into this
    I tried it but it doesn't generate password anymore
     
    Javver, Feb 21, 2021 IP