I am trying to reset some input fields for a login if the loginDiv display is none, however it is not working at all.
Desired Outcome: click the login button, so long as the loginDiv display is none, the two input fields will be reset.
Here is my code:
// LOGIN
const logDiv = document.getElementById('loginDiv');
const wrong = document.getElementById('wrong');
const user = document.getElementById('user');
const pass = document.getElementById('pass');
const log = document.getElementById('logBtn');
log.addEventListener('click', () => {
if (user.value == 'f' && pass.value == 'f') {
logDiv.style.display = 'none';
wrong.style.display = 'none';
} else {
wrong.style.display = 'block';
}
});
// LOGOUT
const logout = document.getElementById('logout');
logout.addEventListener('click', () => {
if (logDiv.style.display == 'none') {
logDiv.style.display = 'block';
}
});
function resetForm() {
if (logDiv.style.display == 'none') {
user.reset();
pass.reset();
}
}
<div id="loginDiv">
<input id="user" type="text" placeholder="username"></input>
<input id="pass" type="password" placeholder="password"></input>
<button id="logBtn" onclick="resetForm()">login</button>
</div>
<div id="wrong" style="display: none;">
<p id="wrongText">incorrect login info.</p>
</div>
<div id="logoutDiv">
<button id="logout" type="button">logout</button>
</div>
display: noneactually removes the element from the DOM, meaning that JavaScript no longer has access it to. Considering your fields are inside the DIV you're trying to remove, what you're trying to achieve is impossible. You can achieve the desired result withvisibility: hiddenif that helps though.display: nonedoesn't remove it from the DOM, it simply sets the CSSdisplayproperty tonone.