0

I have this Javascript code.

var navButton = document.getElementById("nav-button");
let outerlogouttrigger = false;
var email = document.getElementById("Email").value;
var password = document.getElementById("password").value;

document.getElementById("Login").onclick = function() {
  loginfunction()
};

function loginfunction() {
  sessionStorage.setItem('isLoggedin', 'true');

  //This is for making the changes instanteniously after clicking
  if (sessionStorage.getItem('isLoggedin') === 'true') {
    navButton.innerHTML = "Log out";
    navButton.classList.remove("btn-warning");
    navButton.classList.add("btn-success");
    outerlogouttrigger = !outerlogouttrigger; //not woring
  }
}
console.log(outerlogouttrigger);
console.log(email);
console.log(password);
// This if comes in effect after refresh
if (sessionStorage.getItem('isLoggedin') === 'true') {
  navButton.innerHTML = "Log out";
  navButton.classList.remove("btn-warning");
  navButton.classList.add("btn-success");
}

I have 2 questions:

  1. How to change the outerlogouttrigger value which is not changing in my case.
  2. how to get the value of the email and password which is taken input in a html form. The input type of email is email and input type of password is password. I want to check if they are empty or not!

1 Answers1

0

Your code can be VASTLY simplified and that will help you find issues

let isLoggedIn = sessionStorage.getItem('isLoggedin') === 'true';

const setNav = () => {
  const navButton = document.getElementById("nav-button");
  navButton.innerHTML = isLoggedIn ? "Log out" : "Log in";
  navButton.classList.toggle("btn-success",isLoggedIn); // or use add/remove
  navButton.classList.toggle("btn-warning",!isLoggedIn); 
};
const loginfunction = () => {
  // you can even test the navButton text to see if they are loggin in or out here
  const email = document.getElementById("Email").value;
  const password = document.getElementById("password").value;
  if (email.trim() !== "" &&  password.trim() !== "") {
    // do whatever you need to do
    sessionStorage.setItem('isLoggedin', 'true');
  }  
};

window.addEventListener("load",function() { // on page load and all elements exist
  setNav();
  document.getElementById("Login").addEventListener("click",loginfunction);
})
mplungjan
  • 169,008
  • 28
  • 173
  • 236