To secure webpage content under an accordion class with password access, you can implement a password prompt that allows users to view the content only after they enter the correct password. Here’s a complete and precise approach using HTML, CSS, and JavaScript:
1. The HTML Structure: This is an accordion kind of an element, which has the password input form that ought to be clicked. Only after a correct password is given will the inside content of the accordion reveal itself.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Secure Accordion</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<!-- Accordion Button -->
<button class="accordion">Click to View Content</button>
<!-- Accordion Content -->
<div class="accordion-content">
<div id="passwordPrompt">
<label for="pass">Password:</label>
<input type="pass" id="password" />
<button onclick="checkPassword()">Submit</button>
<p id="wrongMessage" style="color: red; display: none;">Wrong password. Please try again.</p>
</div>
<div id="secureContent" style="display: none;">
<p>Content is secure </p>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
2. CSS Styling: Style your accordion button, content and password prompt. Secure content will initially remain hidden and the prompt will be displayed.
/* styles.css */
body {
font-family: Arial, sans-serif;
margin: 20px;
}
.accordion {
background-color: #f1f1f1;
border: none;
padding: 10px;
cursor: pointer;
text-align: left;
width: 100%;
font-size: 18px;
margin: 5px 0;
}
.accordion-content {
padding: 5%;
display: none; /* Hidden by default */
background-color: #f9f9f9;
}
#passwordPrompt {
margin-bottom: 10px;
}
#secureContent {
margin-top: 10%;
padding: 5%;
background-color: #fff;
border: 1px blue;
}
3. JavaScript Logic for Password Verification
Implement the logic to check the entered password and show the secure content if the password is correct.
// script.js
const correctPassword = 'mySecurePassword'; // Set your desired password here
// Handle accordion button click
document.querySelector('.accordion').addEventListener('click', function() {
const content = document.querySelector('.accordion-content');
// Toggle the visibility of the accordion content
content.style.display = content.style.display === 'block' ? 'none' : 'block';
});
// Check the entered password
function checkPassword() {
const passwordInput = document.getElementById('password').value;
const errorMessage = document.getElementById('errorMessage');
const secureContent = document.getElementById('secureContent');
if (passwordInput === correctPassword) {
// Hide password prompt and show secure content
document.getElementById('passwordPrompt').style.display = 'none';
secureContent.style.display = 'block';
} else {
// Show error message if password is incorrect
errorMessage.style.display = 'block';
}
}