JavaScript How to count number of typed character in textarea using javascript? count number of words typed

You could use a closure for the index:

for (var i = 0; i < document.getElementsByClassName("input-content").length; i++) {
    document.getElementsByClassName("input-content")[i].onkeyup = function (i) {
        return function () {
            console.log(this);
            var text_max = 99;
            var text_length = this.value.length;
            var text_remaining = text_max - text_length;
            document.getElementsByClassName("character-count")[i].innerHTML = "Character count:" + text_length + "/" + text_max;
        };
    }(i);
}
<textarea type="text" class="input-content"></textarea>
<p class="character-count"></p>
Answer:1

The issue is that when you are binding the keyupfunction, you are using i as an index for the character-count element. But when you do press a key, i is undefined at that point and so there is no index. You could do it like this, by setting an attribute (I used name) and when you do press a key, get that attribute and use it as an index.

textareas = document.getElementsByClassName("input-content");
charCounts = document.getElementsByClassName("character-count");

for (var i = 0; i < textareas.length; i++) {
  textareas[i].setAttribute("name", i);
  textareas[i].onkeyup = function() {
    var text_max = 99;
    var text_length = this.value.length;
    var text_remaining = text_max - text_length;
    charCounts[this.getAttribute("name")].innerHTML = "Character count: " + text_length + "/" + text_max;
  }
}
textarea {
  height: 300px;
  width: 600px;
  resize: none;
}
<textarea class="input-content"></textarea>
<p class="character-count"></p>
Answer:2

I have a a form with multiple fields on it. Below is example: <input id="txtContactFirstName" name="txtContactFirstName" maxlength="20" class="k-textbox standardInput" required="required" data-...

I have a a form with multiple fields on it. Below is example: <input id="txtContactFirstName" name="txtContactFirstName" maxlength="20" class="k-textbox standardInput" required="required" data-...

  1. jquery change data attribute
  2. jquery change data attribute value
  3. jquery change data attribute on click
  4. jquery change data value dynamically
  5. jquery change data
  6. jquery change data-id value
  7. jquery change data attr
  8. jquery change data-target
  9. jquery change data attr value
  10. jquery change data-content
  11. jquery change data attribute not working
  12. jquery change data-id
  13. jquery change data-original-title
  14. jquery change data-src to src
  15. jquery change data-bind value
  16. jquery change data-toggle
  17. jquery change data-tooltip
  18. jquery change data-placeholder
  19. jquery change data property
  20. jquery change data before form submit

I'm performing following scenario : When I press a button I insert this into the database and that's working. But i don't get feedback if it succeeded or not (the alerts wont pop up). But if I look in ...

I'm performing following scenario : When I press a button I insert this into the database and that's working. But i don't get feedback if it succeeded or not (the alerts wont pop up). But if I look in ...

I am using Grid List(dynamic tiles) in angularJS and using <br/> tag but it is not working Here is the code <div ng-controller="gridListDemoCtrl as vm" flex="" ng-cloak="" class="...

I am using Grid List(dynamic tiles) in angularJS and using <br/> tag but it is not working Here is the code <div ng-controller="gridListDemoCtrl as vm" flex="" ng-cloak="" class="...

Okay, I spent all morning trying to solve this, and despite the fact that it seems to have been a problem for a couple years, I couldn't find a solution that worked. I've seen other people on Stack ...

Okay, I spent all morning trying to solve this, and despite the fact that it seems to have been a problem for a couple years, I couldn't find a solution that worked. I've seen other people on Stack ...

  1. allow chrome to remember password