How do i can program a health bar in JavaScript and HTML | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How do i can program a health bar in JavaScript and HTML

For example a health bar

22nd Nov 2016, 7:45 PM
Diogo Lopes
Diogo Lopes - avatar
33 Answers
+ 4
health:<progress min=0 max=100 value=35 name=hel id=progres></progress> you can change its value in js and color design look. animation shadow in css
23rd Nov 2016, 4:12 AM
Sandeep Chatterjee
+ 2
it is by document.getElementById("progress").value=23;
23rd Nov 2016, 4:48 PM
Sandeep Chatterjee
+ 2
did you used proper id I mean set the progress bar id to progress ok try this <progress id=progress min=0 max=100 name=health value=0></progress> <button onclick="document.getElementById( 'progress').value+=6;">Click</button>
23rd Nov 2016, 5:44 PM
Sandeep Chatterjee
+ 2
yes then add that code to go drop instead of button character looses health... var progressbar=document.getElementById("progress"); function healthdrop(){ health=health-6; progressbar.value=health; healthscore.innerHTML=health; }
23rd Nov 2016, 5:52 PM
Sandeep Chatterjee
+ 2
yes you can put any variable as value
23rd Nov 2016, 5:55 PM
Sandeep Chatterjee
+ 2
I got the error you used progressbar=document. getElementById("progress").value now in line 3 you set its value so it becomes progressbarselement.value.value use value once
23rd Nov 2016, 6:13 PM
Sandeep Chatterjee
+ 2
no I had no problem with German bien but code is not being shown did you corrected that you should use document.getElementById("progress"); only not ("progress").value;
23rd Nov 2016, 6:28 PM
Sandeep Chatterjee
+ 2
because you used .value twice first health=document...("progress").value then you set Heath.value so it becomes element.value.value use only once
23rd Nov 2016, 6:34 PM
Sandeep Chatterjee
+ 2
hey Diego what I suggest is you make a separate page please for testing this <progress id=progress min=0 max=100 name=health value=0></progress> <button onclick="function(){ document.getElementById('progress').value+=6; }">Click</button> or <button onclick=incprogress()>Click</button> <script>function incprogress(){ document.getElementById("progress").value+=6;}</scrip t>
23rd Nov 2016, 6:41 PM
Sandeep Chatterjee
+ 2
the spelling of heathbar at innerHTML is also wrong correct it. one suggestion that instead of alert prompt there you can use <input type=text id=input> and then access's it using js document.getElementById("input").value;
23rd Nov 2016, 6:46 PM
Sandeep Chatterjee
+ 2
ok I will answer tomorrow it is too night here I will see it in morning zzz...
23rd Nov 2016, 6:50 PM
Sandeep Chatterjee
+ 2
i live in India 🇮 ndia
23rd Nov 2016, 7:09 PM
Sandeep Chatterjee
+ 2
in health bar alpha you declared variable called health bar and set value of not heathbar but progress which is showing error change it
24th Nov 2016, 3:16 AM
Sandeep Chatterjee
+ 2
I too was getting same error in my program it is because var element=document.getElementById(element) doesn't work in sololearn so you have to replace it with document.getElementById( directly I don't why but you can see my code
24th Nov 2016, 6:10 PM
Sandeep Chatterjee
+ 2
there is only one code as public see it's js section I made the var comment there is other problem too wait I will see all your code I can't now becuase it's time to sleep in India 😀
24th Nov 2016, 8:29 PM
Sandeep Chatterjee
+ 2
hey Diego problem is solved move the line var=hpyou before the function and make the attack 4e function
26th Nov 2016, 3:09 AM
Sandeep Chatterjee
+ 1
Thanks
23rd Nov 2016, 4:29 PM
Diogo Lopes
Diogo Lopes - avatar
+ 1
How I can change the value with JavaScript?😅
23rd Nov 2016, 4:40 PM
Diogo Lopes
Diogo Lopes - avatar
+ 1
Thanks twice
23rd Nov 2016, 5:14 PM
Diogo Lopes
Diogo Lopes - avatar
+ 1
I've written this and it don't work: var hpyou = 100 var you = document.getElementById("progress").value = hpyou <progress min="0" max="100"></progress>
23rd Nov 2016, 5:25 PM
Diogo Lopes
Diogo Lopes - avatar