HTML:
<!DOCTYPE html>
<html>
<head>
<title>Password Generator</title>
</head>
<body>
<h1>Password Generator</h1>
<button id="generate">Generate Password</button>
<input type="text" id="password" readonly>
<script>
// Function to generate a random password
function generatePassword(length) {
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()-_=+";
let password = "";
for (let i = 0; i < length; i++) {
const randomIndex = Math.floor(Math.random() * charset.length);
password += charset[randomIndex];
}
return password;
}
// Function to update the password input field
function updatePasswordField() {
const passwordField = document.getElementById("password");
const passwordLength = 12; // You can change this to your desired password length
passwordField.value = generatePassword(passwordLength);
}
// Attach the function to the "Generate Password" button
const generateButton = document.getElementById("generate");
generateButton.addEventListener("click", updatePasswordField);
// Generate an initial password when the page loads
updatePasswordField();
</script>
</body>
</html>