最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to getchange value of HTML5 progress bar? - Stack Overflow

programmeradmin3浏览0评论

Below is the html and js I have for my progress bar: But my console is telling me the value of the variable dragonHealth is null when it should be 100 as initialized. From what I have found, the value should be a floating point variable so why is it ing out as null? Not too familiar with this progress tag btw. Thanks for the help. Btw, the health ID in my css is just a blank one created just to reference it in js.

JS

var dragonHealth = document.getElementById("health").value;

html

<progress id="health" value="100" max="100"></progress>

Below is the html and js I have for my progress bar: But my console is telling me the value of the variable dragonHealth is null when it should be 100 as initialized. From what I have found, the value should be a floating point variable so why is it ing out as null? Not too familiar with this progress tag btw. Thanks for the help. Btw, the health ID in my css is just a blank one created just to reference it in js.

JS

var dragonHealth = document.getElementById("health").value;

html

<progress id="health" value="100" max="100"></progress>
Share Improve this question edited Apr 20, 2017 at 16:28 Gabriele Petrioli 196k34 gold badges271 silver badges328 bronze badges asked Apr 20, 2017 at 16:26 indora_no_yaindora_no_ya 651 gold badge1 silver badge5 bronze badges 1
  • works fine here fiddle.jshell/umbkuzyj . (check the console) – Getter Jetter Commented Apr 20, 2017 at 16:30
Add a ment  | 

1 Answer 1

Reset to default 11

Make sure the JS is run after the DOM is ready (or at the end of the page)

document.addEventListener("DOMContentLoaded", function(event) {
  /* DOM is ready, so we can query for its elements */
  var dragonHealth = document.getElementById("health").value;
  console.log(dragonHealth);
  
  /*additional code for ment*/
  document.querySelector('button').addEventListener("click", function(event){
    document.getElementById("health").value += 5;
  });
})
<progress id="health" value="60" max="100"></progress>
<button>change progress value</button>

发布评论

评论列表(0)

  1. 暂无评论