javascript自动生成随机字符串

I want the random string result(output) to display inside the text box so I can saved the value into my database.

(function() {
    function IDGenerator() {     
        this.length = 4;
        this.timestamp = +new Date;
         
        var _getRandomInt = function( min, max ) {
            return Math.floor( Math.random() * ( max - min + 1 ) ) + min;
        }
         
        this.generate = function() {
            var ts = this.timestamp.toString();
            var parts = ts.split( "" ).reverse();
            var id = "TEST";
             
            for( var i = 0; i < this.length; ++i ) {
                var index = _getRandomInt( 0, parts.length - 1 );
                id += parts[index];  
            }
             
            return id;
        }
    }
     
    document.addEventListener( "DOMContentLoaded", function() {
        var btn = document.querySelector( "#generate" ),
            output = document.querySelector( "#output" );
            
        btn.addEventListener( "click", function() {
            var generator = new IDGenerator();
            output.innerHTML = generator.generate();
        }, false); 
    });
})();
<p><button id="generate">Generate</button></p>
<p><code id="output"></code></p>    

NOTE

Any idea on how to fix this? So when every time I click generate button the result will come out in a text box then save the value to my database.

</div>

(function() {
     function IDGenerator() {
     
         this.length = 4;
         this.timestamp = +new Date;
         
         var _getRandomInt = function( min, max ) {
            return Math.floor( Math.random() * ( max - min + 1 ) ) + min;
         }
         
         this.generate = function() {
             var ts = this.timestamp.toString();
             var parts = ts.split( "" ).reverse();
             var id = "TEST";
             
             for( var i = 0; i < this.length; ++i ) {
                var index = _getRandomInt( 0, parts.length - 1 );
                id += parts[index];  
             }
             
             return id;
         }

         
     }
     
     
     document.addEventListener( "DOMContentLoaded", function() {
        var btn = document.querySelector( "#generate" ),
            output = document.querySelector( "#output" );
            
        btn.addEventListener( "click", function() {
            var generator = new IDGenerator();
            $("#output").val(generator.generate());
        }, false); 
         
     });
     
     
 })();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p><button id="generate">Generate</button></p>
    <input type="" id="output" name="">

</div>

Simply you can replace code tag by input tag

<p><input id="output" type="text" value="" /></p>

and replace output.innerHTML to be

output.value= generator.generate();

Hope that's help