I've been attempting to create a password-only form that reveals a hidden div on the same page when the correct password is entered. I want to avoid reloading the page, but I'm unsure of how to achieve this.
function checkPassword() {
var confirmPassword = "admin";
var password = document.getElementById("pass").value;
if (password == confirmPassword) {
var x = document.querySelector('.hidden');
if (x.style.display === "none") {
x.style.display = "block";
} else {
alert('Try again.');
x.style.display = "none";
}
}
}
.hidden {
display: none;
}
<form method="post">
<label for="pswd">Enter Password</label>
<input type="password" id="pass">
<input type="button" value="Go" onclick="checkPassword();" /></form>
<div class="hidden">
<p>This section should only be visible to users who know the password.</p>
</div>
I suspect that the JavaScript code may be incorrect. Are there any alternative methods to accomplish this task?