textArea Length Limiter

Prevent more than maxLength characters form being typed in a textArea

by David McClelland

HTML

<textarea property="description" style="width: 300px; height: 120px" styleId="description" styleClass="text" keydown="maxLength()" id="description"></textarea>

JavaScript

document.addEventListener("keydown", keyDownTextField, false);

function maxLength(el) {
    alert("maxLength here");
    var max = 5;
    if (el.text.length >= max) {
        alert("Max reached!");
        return false;
    }
}

function keyDownTextField(e) {
    theTextArea = document.getElementById("description");
    var len = theTextArea.value.length;
    var max = 5;
    if (len >= max) {
        alert("Max reached!");
        return false;
    }
}