I was trying to show battery level using javascript where I found an unexpected error. My codes:
const percentage = document.getElementById('percentage');
const percent = document.getElementById('percent');
navigator.getBattery().then(function(battery){
percentage.style.width = battery.level * 100 + '%' ;
percent.innerHTML = battery.level * 100 + '%';
alert(battery.level);
});
I got an error saying, >TypeError: cannot read the proper 'style' of null When I try putting the 4th and 5th line on the comment e.g.
//
percentage.style.width = battery.level * 100 + '%' ;
First, I got another error message saying:
TypeError: Cannot set the property 'innerHTML' of null Then, the alert() gave the correct battery level