JSFiddle - React, Tailwind, and code Playground

HTML

<form>
<input type="button" value="Generate Coupon" id="button" onclick="populateform(this.form.thelength.value);">
<input type="hidden" name="thelength" size=3 value="6">     
<input name="COUPON" type="text" id="COUPON" size="20" maxlength="20">
    </form>

JavaScript

var keylist="abcdefghijklmnopqrstuvwxyz123456789";
var temp="";
function generatecoupon(plength){
    temp = "";
    
    for (i=0;i<plength;i++)
        temp+=keylist.charAt(Math.floor(Math.random()*keylist.length));
    
    return temp;
}

function populateform(enterlength){
   document.getElementById("COUPON").value=generatecoupon(enterlength);
}