JavaScript Characters Count Functions

JavaScript characters Counter script. onClick button count characters, textarea onInput (paste change keyup) count characters

1

JavaScript on Click button count characters

0
JavaScript
document.getElementById("count-btn").onclick = function() {
    var total_length = document.getElementById("user-text").value.length;
    document.getElementById("char-length").innerText = total_length;
};
HTML
<textarea id="user-text" rows="10" cols="50"></textarea>
<button id="count-btn">count characters</button>
<div id="char-length"></div>
Try it Yourself
2

JavaScript on change function count characters

0
JavaScript
document.getElementById("user-text").onchange = function() {
    var total_length = this.value.length;
    document.getElementById("char-length").innerText = total_length;
};
HTML
<textarea id="user-text" rows="10" cols="50"></textarea>
<div id="char-length"></div>
Try it Yourself
3

on input function (paste change keyup) count characters

0
JavaScript
document.getElementById("user-text").oninput = function() {
    var total_length = this.value.length;
    document.getElementById("char-length").innerText = total_length;
};
HTML
<textarea id="user-text" rows="10" cols="50"></textarea>
<div id="char-length"></div>
Try it Yourself
4

Set limit characters length

100
JavaScript
(function() {
    let limit = 100;
    let textaea = document.getElementById("user-text");
    textaea.setAttribute("maxlength", limit);

    textaea.oninput = function() {
        textaea.setAttribute("maxlength", limit);
        let total_length = this.value.length;
        document.getElementById("char-length").innerText = limit - total_length;
    };

})();
HTML
<textarea id="user-text" rows="10" cols="50"></textarea>
<div id="char-length"></div>
Try it Yourself