Skip to content

Progress not set correctly when exceeding textarea max character limit (demo) #5

Open
@ConnorKrammer

Description

@ConnorKrammer

Small issue with the progress bar not filling all the way on repeated text input.

To reproduce:

  1. Focus the textarea demo.
  2. Type until the progress bar is 3/4 full.
  3. Stop, and note that the progress bar does fill up correctly.
  4. Now switch to a new line, and hold down any key. Characters will be inserted as long as the key is held down, and extra characters will be erased on keyup. However, the progress bar will not update correctly to 100% in this scenario.

On a related note, it might be prudent not to allow characters to be inserted at all if the character limit is exceeded -- binding to the 'keypress' event and disallowing any keypress except the backspace key would work. Something like:

var maxChars = 200;
var keyCodeBackspace = 8;
var keyCodeDelete = 46;

// Prevent input when textarea character limit exceeded. Allow deleting characters.
textarea.addEventListener('keypress', function(event) {
    if (textarea.value.length >= maxChars
      && event.keyCode !== keyCodeBackspace
      && event.keyCode !== keyCodeDelete) {
        event.preventDefault();
    }
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions