简体   繁体   中英

Can HTML5 meter value accept setAttribute?

How should I go about pushing the value of a variable to an input value with Javascript? I know is possible to set other element attributes with setAttribute but I can't get anything to set in the "value".

Here's the current javascript function:

function testfunc(){
    document.getElementsByTagName("meter")[0].setAttribute("value", "2000");   
}​

And the HTML:

<!doctype html>
<head>
<meta charset="utf-8">
<title>untitled</title>
</head>
<body>

<div id="container">
    <button onclick="testfunc()">Find my elevations</button>  
    <meter value="900" min="0" max="5280" id="high">500/5280</meter>
</div> 

</body>
</html>

What i'd like to do is replace the current set value of 900 with the testfunc() value of 2000. The 2000 is completely arbitrary as it will be a dynamically generated number in the live app.

HTML 5: 4.10.17 The meter element suggests that you can just do

document.getElementById("high").value = /* float value between min and max */

The relevant portions of that spec are

The following inequalities must hold, as applicable:

  • minimum ≤ value ≤ maximum
  • minimum ≤ low ≤ maximum (if low is specified)
  • minimum ≤ high ≤ maximum (if high is specified)
  • minimum ≤ optimum ≤ maximum (if optimum is specified)
  • low ≤ high (if both low and high are specified)

...

The value IDL attribute, on getting, must return the actual value. On setting, the given value must be converted to the best representation of the number as a floating point number and then the value content attribute must be set to that string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM