<div class="pw-generator">
<div class="pw-display">
<input type="text" id="password" readonly placeholder="Generated Password">
<button id="copyPass" aria-label="Copy">
<svg viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="9" y="9" width="13" height="13" rx="2" ry="2"></rect><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"></path></svg>
</button>
</div>
<button id="genBtn" class="gen-btn">Generate Password</button>
</div>
.pw-generator {
font-family: system-ui, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
max-width: 350px;
margin: 0 auto;
background: #fff;
padding: 30px;
border-radius: 16px;
box-shadow: 0 4px 20px rgba(0,0,0,0.05);
}
.pw-display {
display: flex;
background: #f3f4f6;
padding: 5px;
border-radius: 8px;
margin-bottom: 20px;
border: 1px solid #e5e7eb;
}
#password {
flex: 1;
background: transparent;
border: none;
padding: 10px;
font-family: monospace;
font-size: 1.1rem;
color: #1f2937;
outline: none;
}
#copyPass {
background: white;
border: 1px solid #d1d5db;
border-radius: 6px;
width: 40px;
cursor: pointer;
color: #4b5563;
display: flex;
align-items: center;
justify-content: center;
}
#copyPass:hover {
color: #2563eb;
border-color: #3b82f6;
}
.gen-btn {
width: 100%;
padding: 12px;
background: #2563eb;
color: white;
border: none;
border-radius: 8px;
font-weight: 600;
cursor: pointer;
transition: background 0.2s;
}
.gen-btn:hover {
background: #1d4ed8;
}
const passwordInput = document.getElementById("password");
const genBtn = document.getElementById("genBtn");
const copyBtn = document.getElementById("copyPass");
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()";
function generatePassword() {
let password = "";
for (let i = 0; i < 12; i++) {
const randomIndex = Math.floor(Math.random() * chars.length);
password += chars[randomIndex];
}
passwordInput.value = password;
}
genBtn.addEventListener("click", generatePassword);
copyBtn.addEventListener("click", () => {
passwordInput.select();
document.execCommand("copy");
});