Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to change <meter> values?

I'm a beginner at HTML, and while learning about HTML5 I've found a cool tool, the <meter>. However, it won't update; it's there as a static value!

My question is simple: how do I use the length of a <textarea> to change the color of <meter>, so that the user will, for example, see red when he reaches 160 characters (the maximum value)? In other words, count the <textarea> characters, and send them to the value of the meter tag.

like image 453
Abdelouahab Pp Avatar asked Oct 29 '25 09:10

Abdelouahab Pp


2 Answers

Note that not all browser will support this tag. E.g. no support by IE until IE10. http://caniuse.com/#search=meter.

Something like this should work:

HTML

<textarea id="sometext"></textarea>
<meter value="10" min="0" max="160" id="somemeter">2 out of 160</meter>​

JS

(function() {
    var textarea = document.getElementById('sometext');
    var meter = document.getElementById('somemeter');

    var theLength = 0;

    textarea.addEventListener('keypress', function() {
        theLength = textarea.value.length;

        if (theLength > 160) {
            theLength = 160;
        }

        meter.value = theLength;
    });
})();​

Demo: http://jsfiddle.net/RBUmQ/1/

like image 200
PeeHaa Avatar answered Oct 31 '25 01:10

PeeHaa


if you use jquery

$("#meter_id").val($("my_text_area_id").val().length)

at least i think .... something like that anyway

like image 26
Joran Beasley Avatar answered Oct 31 '25 00:10

Joran Beasley



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!