HTML element to use for character count
Optional
config: CharacterCountConfig = {}Character count config
Protected
_$rootPrivate
$screenPrivate
$textareaPrivate
$visiblePrivate
i18nPrivate
lastPrivate
lastPrivate
maxPrivate
valueStatic
Constant
defaultsCharacter count default config
Static
elementStatic
moduleName for the component used when initialising using data-module attributes.
Static
Constant
schemaCharacter count config schema
Protected
$rootProtected
Returns the root element of the component
Protected
configProtected
Returns the root element of the component
Internal
Character count config override
To ensure data-attributes take complete precedence, even if they change
the type of count, we need to reset the maxlength
and maxwords
from
the JavaScript config.
configuration specified by dataset
Private
bindPrivate
Bind change events
Set up event listeners on the $textarea so that the count messages update when the user types.
Private
countPrivate
Count the number of characters (or words, if config.maxwords
is set)
in the given text
The text to count the characters of
the number of characters (or words) in the text
Private
formatPrivate
Formats the message shown to users according to what's counted and how many remain
The number of words/characaters remaining
"words" or "characters"
Status message
Private
getPrivate
Get count message
Status message
Private
handlePrivate
Handle blur event
Stop checking the textarea value once the textarea no longer has focus
Private
handlePrivate
Handle focus event
Speech recognition software such as Dragon NaturallySpeaking will modify
the fields by directly changing its value
. These changes don't trigger
events in JavaScript, so we need to poll to handle when and if they occur.
Once the keyup event hasn't been detected for at least 1000 ms (1s), check if the textarea value has changed and update the count message if it has.
This is so that the update triggered by the manual comparison doesn't conflict with debounced KeyboardEvent updates.
Private
handlePrivate
Handle key up event
Update the visible character counter and keep track of when the last update happened for each keypress
Private
isPrivate
Check if count is over threshold
Checks whether the value is over the configured threshold for the input. If there is no configured threshold, it is set to 0 and this function will always return true.
true if the current count is over the config.threshold (or no threshold is set)
Private
updatePrivate
Update count message
Helper function to update both the visible and screen reader-specific counters simultaneously (e.g. on init)
Private
updatePrivate
Update count message if textarea value has changed
Private
updatePrivate
Update screen reader count message
Private
updatePrivate
Update visible count message
Static
check
Character count component
Tracks the number of characters or words in the
.govuk-js-character-count
<textarea>
inside the element. Displays a message with the remaining number of characters/words available, or the number of characters/words in excess.You can configure the message to only appear after a certain percentage of the available characters/words has been entered.