HTML, Battery Level |
HTML, Battery Level |
s1lv3rf0x87 |
Jan 18 2017, 06:38 AM
Post
#1
|
Group: Members Posts: 5 Joined: 18-January 17 Member No.: 26,270 |
Hey Guys,
I have some HTML code which will display the battery status of a device and display the charge as a decimal value. I am trying to tweak the code so it displays the charge value as a percentage and not a decimal value. Can someone have a look at this and advise if this is possible and if so how to do this. Many Thanks in advance. CODE <html> <head> <title>Battery Status API Example</title> <script> window.onload = function () { function updateBatteryStatus(battery) { document.querySelector('#charging').textContent = battery.charging ? 'charging' : 'not charging'; document.querySelector('#level').textContent = battery.level; document.querySelector('#dischargingTime').textContent = battery.dischargingTime / 60; } navigator.getBattery().then(function(battery) { // Update the battery status initially when the promise resolves ... updateBatteryStatus(battery); // .. and for any subsequent updates. battery.onchargingchange = function () { updateBatteryStatus(battery); }; battery.onlevelchange = function () { updateBatteryStatus(battery); }; battery.ondischargingtimechange = function () { updateBatteryStatus(battery); }; }); }; </script> </head> <body> <div id="charging">(charging state unknown)</div> <div id="level">(battery level unknown)</div> <div id="dischargingTime">(discharging time unknown)</div> </body> </html> |
pandy |
Jan 18 2017, 07:04 AM
Post
#2
|
🌟Computer says no🌟 Group: WDG Moderators Posts: 20,733 Joined: 9-August 06 Member No.: 6 |
Can't you just multiply the value with 100? I don't follow the script, assume there is more to it than shown here? But I'm guessing battery.level * 100 .
And this is JavaScript, not HTM. |
s1lv3rf0x87 |
Jan 18 2017, 07:12 AM
Post
#3
|
Group: Members Posts: 5 Joined: 18-January 17 Member No.: 26,270 |
Can't you just multiply the value with 100? Hi Pandy, Thank you for your response. My HTML skills are quite poor at the moment and I am still learning. I managed to find this code which works but I need a percentage not a decimal value. I am not sure what part of the code I should be multiplying by 100 in order to get it to display as a percentage. If you could advise or give a working solution that would be ideal. Thanks |
pandy |
Jan 18 2017, 07:15 AM
Post
#4
|
🌟Computer says no🌟 Group: WDG Moderators Posts: 20,733 Joined: 9-August 06 Member No.: 6 |
Sorry, I edited my post above but you had time to post while I did.
I'm guessing you should do it here, but I don't understand the script, so just a guess. CODE document.querySelector('#level').textContent = battery.level * 100; |
s1lv3rf0x87 |
Jan 18 2017, 07:22 AM
Post
#5
|
Group: Members Posts: 5 Joined: 18-January 17 Member No.: 26,270 |
Sorry, I edited my post above but you had time to post while I did. I'm guessing you should do it here, but I don't understand the script, so just a guess. CODE document.querySelector('#level').textContent = battery.level * 100; Hi Pandy, Thanks for that I have done what you have suggested and it now displays the value without the percentage sign. This is a step in the right direction I just need the percentage sign to be displayed now. I am guessing it will be in this same location but not sure how to format. |
pandy |
Jan 18 2017, 07:40 AM
Post
#6
|
🌟Computer says no🌟 Group: WDG Moderators Posts: 20,733 Joined: 9-August 06 Member No.: 6 |
OK, a little JS lesson in place then. You quote string values but not variables and operators. You can concatenate the two with +.
So try this. CODE document.querySelector('#level').textContent = battery.level * 100 + ' %'; Added a space before the percent sign. Remove the space if you don't want it like that. + is both a math operator and and a string operator used to concatenate strings. JavaScript understands from the context how to use it. If we have this... var foo = 100 + 100 + ' $'; alert(foo); ... JavaScript will add the two numbers and concatenate the result with the dollar sign. Clever huh? |
s1lv3rf0x87 |
Jan 18 2017, 07:49 AM
Post
#7
|
Group: Members Posts: 5 Joined: 18-January 17 Member No.: 26,270 |
OK, a little JS lesson in place then. You quote string values but not variables and operators if you do some math. You can concatenate the two with +. + is both a math operator and and a string operator used to concatenate strings. JavaScript understands from the context how to use it. So try this. CODE document.querySelector('#level').textContent = battery.level * 100 + ' %'; Added a space before the percent sign. Remove the space if you don't want it like that. Thanks Pandy you are a star. That works perfectly. I don't suppose you could help me out tweak this code a little further. I have been asked to change the display charge status so when the battery level reaches 100% to display Fully Charged. Is that something you could help me with? |
pandy |
Jan 18 2017, 08:36 AM
Post
#8
|
🌟Computer says no🌟 Group: WDG Moderators Posts: 20,733 Joined: 9-August 06 Member No.: 6 |
I have been asked to change the display charge status so when the battery level reaches 100% to display Fully Charged. Is that something you could help me with? I think this would work. Only the #level line is changed. I moved it last for clearity CODE document.querySelector('#charging').textContent = battery.charging ? 'charging' : 'not charging'; document.querySelector('#dischargingTime').textContent = battery.dischargingTime / 60; if (battery.level != 100) document.querySelector('#level').textContent = battery.level * 100 + ' %'; else document.querySelector('#level').textContent = 'Fully Charged'; |
Lo-Fi Version | Time is now: 27th April 2024 - 10:51 AM |