index.html
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>ランダムな文字列発生させるサイト</title>
<link
rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/5.0.0-alpha1/css/bootstrap.min.css"
integrity="sha384-r4NyP46KrjDleawBgD5tp8Y7UzmLA05oM1iAEQ17CSuDqnUK2+k9luXQOfXJCJ4I"
crossorigin="anonymous"
/>
<style>
body {
margin: 0;
background: #f7f7f7;
}
#wrap {
overflow: hidden;
}
main {
max-width: 750px;
margin: 0 auto;
}
</style>
</head>
<body>
<div id="wrap">
<br />
<main>
<div class="input-group">
<input type="number" id="num" class="form-control" value="8" />
<div class="input-group-append">
<button id="sumbit" class="btn btn-primary" type="button">
作成
</button>
</div>
</div>
<br />
<div class="input-group">
<input id="text" class="form-control" readonly="" />
<div class="input-group-append">
<button id="copy" class="btn btn-success" type="button">
コピー
</button>
</div>
</div>
</main>
</div>
<script
src="https://stackpath.bootstrapcdn.com/bootstrap/5.0.0-alpha1/js/bootstrap.min.js"
integrity="sha384-oesi62hOLfzrys4LxRF63OJCXdXDipiYWBnvTl9Y9/TRlw5xlKIEHpNyvvDShgf/"
crossorigin="anonymous"
></script>
<script>
document.getElementById('sumbit').onclick = () => {
const _s = 'abcdefghijklmnopqrstuvwxyz0123456789';
const _ret = [...Array(Number(document.getElementById('num').value))]
.map(() => _s[Math.floor(Math.random() * _s.length)])
.join('');
document.getElementById('text').value = _ret;
};
document.getElementById('copy').onclick = () => {
document.getElementById('text').select();
document.execCommand('copy');
};
</script>
</body>
</html>