lundi 13 mai 2019

On refresh, checked = true based on firebase value

I have an aplication connected to a firebase realtime. I can set data to the firebase with my checkbox, but im facing problems to set the atribute checked based on firebase data.

MY JS CODE

firebase.database().ref('0001/switches/001').on('value', snapshot => {

            var chValue = snapshot.val();

            console.log(chValue);

            if (chValue == true) {
                document.getElementById("sch1").checked = true;
            } else {
                document.getElementById("sch1").checked = false;
            }                
        });

I confirme that the chValue is working based on firebase data, using the console.log() but, I dont know why...

THIS

document.getElementById("sch1").checked = true;

Is not working! If i put outside the firebase function it works, I could find the reason.

THIS WORK

 firebase.database().ref('0001/switches/001').on('value', snapshot => {

            var chValue = snapshot.val();

            consolo.log(chValue);

            if (chValue == true) {
                document.getElementById("sch1").checked = true;
            } else {
                document.getElementById("sch1").checked = false;
            }            
        });

//Outside firebase function
        document.getElementById("sch1").checked = true;




Aucun commentaire:

Enregistrer un commentaire