javascripthtmlvariablesmeter

How does using variables in javascript work?


I have some simple code here, and I want to test it out, by making the display none, is there something wrong with my javascript? (I'm very new to js)

 var meter = document.getElementById("meter").innerHTML;
        meter.style.display = "none";
<meter id="meter" min="0" max="10"></meter>

The console says meter is undefined, but I defined it right above.


Solution

  • meter is the innerHTML of the element (which is a string), not the element itself.

    Instead, use:

    var meter = document.getElementById("meter");
    meter.style.display = "none";
    <meter id="meter" min="0" max="10"></meter>

    If you want to hide it and get its value, you can do:

    var meter = document.getElementById("meter");
    meter.style.display = "none";
    var val = meter.value;
    console.log(val)
    <meter id="meter" min="0" max="10" value="3"></meter>