Return to Snippet

Revision: 31034
at August 27, 2010 18:19 by cfleschhut


Initial Code
function textCounter(field, countfield, maxlimit) {
    if (field.value.length > maxlimit) {
        field.value = field.value.substring(0, maxlimit);
    } else {
        countfield.value = maxlimit - field.value.length;
    }
}

window.onload = function() {
    var ta = document.getElementById("message"),
        inp = document.getElementById("remLen"),
        limit = 50;
    inp.value = limit;
    ta.onkeyup = function() {
        textCounter(this, inp, limit);
    }
}


<form>
    <p>
        <textarea id="message" cols="40" rows="8"></textarea>
    </p>
    <p>
        <label><input type="text" id="remLen" size="3" /> remaining characters</label>
    </p>
</form>

Initial URL
http://jsfiddle.net/BdRn2/

Initial Description
different solution by PPK:\\r\\n[http://www.quirksmode.org/book/examplescripts/maxlength/index.html](http://www.quirksmode.org/book/examplescripts/maxlength/index.html)

Initial Title
Basic Textarea Remaining Characters Script

Initial Tags
forms

Initial Language
JavaScript