DEV Community

Alessia Volpi
Alessia Volpi

Posted on

GetElementbyId

Hi, i'm a new javascript programmer. I'm coding a simple Web site but I have a problem with getelementbyid. I have connected the site to firebase and I'm Tring to modify some element after login. When the program in a file.js change page from access.html to profile.html the console shoe me an error about the element in access. Getelementbyid return null when I am in profile.html and equally in access.html I don't know how to solve the problem...please help

Top comments (4)

Collapse
 
_bkeren profile image
''

can you share the code?

Collapse
 
alessiav8 profile image
Alessia Volpi

import { initializeApp } from "gstatic.com/firebasejs/9.4.0/fireb...";
import { getDatabase, set, ref, update } from "gstatic.com/firebasejs/9.4.0/fireb...";
import { getAuth, signOut, createUserWithEmailAndPassword, signInWithEmailAndPassword,onAuthStateChanged } from "gstatic.com/firebasejs/9.4.0/fireb...";
import * as func2 from "./func2.js";

const firebaseConfig = {
apiKey: "AIzaSyA0PdI6RRM_VqyxEsUYuPe0Gu_TUrbbuuQ",
authDomain: "beeteam-2a5f7.firebaseapp.com",
databaseURL: "beeteam-2a5f7-default-rtdb.firebas...",
projectId: "beeteam-2a5f7",
storageBucket: "beeteam-2a5f7.appspot.com",
messagingSenderId: "223479016271",
appId: "1:223479016271:web:383e9319470cfaf4bd733a",
measurementId: "G-04ERX78GQJ"
};

// Initialize Firebase
export const app= initializeApp(firebaseConfig);
const database = getDatabase(app);
const auth=getAuth();

let sub=document.getElementById("sub");
let log=document.getElementById("login");
let out=document.getElementById("logout");

sub.addEventListener('click', (e) =>{

var nome=document.getElementById("nreg").value;
var cognome=document.getElementById("creg").value;
var numero=document.getElementById("numreg").value;
var email=document.getElementById("ereg").value;
var password=document.getElementById("preg").value;

createUserWithEmailAndPassword(auth, email, password,nome,cognome,numero)
.then((userCredential) => {

   const user = userCredential.user;
   set(ref(database,'users/' + user.uid),{
       email : email,
       password:password,
       nome: nome,
       cognome:cognome,
       telefono:numero,
       imageURL: "nofoto.jpg"
   });
   alert("User Create! Now Log in");
Enter fullscreen mode Exit fullscreen mode

})
.catch((error) => {
const errorCode = error.code;
const errorMessage = error.message;
alert(errorMessage);
});
});

log.addEventListener('click', (e) => {

var email=document.getElementById("emailacc").value;
var password=document.getElementById("pacc").value;

signInWithEmailAndPassword(auth, email, password)
.then((userCredential) => {

const user = userCredential.user;
const dt= new Date();
update(ref(database,'users/' + user.uid),{
last_login : dt,
})
alert("User Loged in!");

   window.location.href="profilo.html";
   window.onload= func2.openProfile();

   //document.getElementById("c1").textContent="CIAOOOOO"
Enter fullscreen mode Exit fullscreen mode

})
.catch((error) => {
const errorCode = error.code;
const errorMessage = error.message;
alert(errorMessage);
});

});

Collapse
 
alessiav8 profile image
Alessia Volpi • Edited

the console show me this error only after the change of the page

Photo error:
dev-to-uploads.s3.amazonaws.com/up...

Collapse
 
_bkeren profile image
''

you can add null check
if ( sub != null) {
//code
}