How to secure webpage content under the accordion class with password access

0 votes

How to secure webpage content under the accordion class with password access?

Explain how to secure content by using JavaScript to prompt for a password when an accordion section is clicked. Mention validating the password before revealing the content and optionally implementing server-side validation for enhanced security.

Nov 26, 2024 in Web Development by Nidhi
• 8,520 points
73 views

1 answer to this question.

0 votes

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';

  }

}

answered Dec 31, 2024 by Navya

Related Questions In Web Development

+1 vote
1 answer

How to access the Angularjs scope of a particular html element from our console?

Hello, You should follow the below steps:-- 1.Compile and ...READ MORE

answered Jan 21, 2020 in Web Development by Niroj
• 82,840 points

edited Jan 21, 2020 by Niroj 3,168 views
0 votes
1 answer

html5 video with jquery and inview - how to point to the right video element?

You can make use of this. For more ...READ MORE

answered Aug 5, 2022 in Web Development by rajatha
• 7,680 points
2,115 views
0 votes
0 answers

How do I chain asynchronous AJAX calls with promises in jQuery to ensure proper order when modifying the DOM?

How do I chain asynchronous AJAX calls ...READ MORE

Nov 13, 2024 in Web Development by Nidhi
• 8,520 points
181 views
0 votes
1 answer
+4 votes
9 answers

***IMPORTANT*** AngularJS Interview Questions.

Yes, I agree with Omkar AngularJs is ...READ MORE

answered Mar 17, 2019 in Career Counselling by Sharad
• 180 points
3,883 views
0 votes
0 answers

Cannot refresh AWS Web console during EC2 reboot

We were relying on monitoring (and refreshing) ...READ MORE

Oct 17, 2018 in AWS by findingbugs
• 4,780 points
767 views
0 votes
1 answer

Sending Web requests from my Web server to my IoT device

You need to start an HTTP server ...READ MORE

answered Jan 8, 2019 in IoT (Internet of Things) by nirvana
• 3,130 points
2,227 views
0 votes
1 answer

How to run Nutch in Hadoop installed in pseudo-distributed mode

Make sure you have built Nutch from ...READ MORE

answered Jan 24, 2019 in Big Data Hadoop by Frankie
• 9,830 points
1,032 views
0 votes
1 answer

How to implement a secure REST API with node.js?

Step 1: Setting Up Your Development Environment Install ...READ MORE

answered Dec 13, 2024 in Web Development by Navya
115 views
0 votes
1 answer

How do I send a file from postman to node.js with multer?

npm install multer express Then  we will set ...READ MORE

answered Oct 24, 2024 in Web Development by kavya

edited Oct 30, 2024 by Nidhi 310 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP