In this section of the documentation, an asterisk (*) denotes a method argument that is optional. Defaults are noted for all optional arguments.
This method enables full live-counting on an element. Use the callback function to process the counted values.
const area = document.getElementById('area')
Countable.on(area, counter => console.log(counter))
In order to remove the Countable functionality from an element, call Countable.off()
.
const area = document.getElementById('area')
Countable.off(area)
There might be situations where you only want to count paragraphs, words and characters of an element once, i.e. if you want to display the word count of an article on your blog. In order to achieve this, call Countable.count()
.
This method is aliased as Countable.count()
.
const area = document.getElementById('area')
Countable.count(area, counter => console.log(counter))
Countable also provides a handy method to check if live-counting functionality is enabled on an element.
const area = document.getElementById('area')
Countable.enabled(area)
Both Countable.on()
and Countable.count()
require a callback function. Your callback function is called with a single parameter, an object containing the following properties.
You can refer back to your element using this
inside the callback function.
Countable.on()
and Countable.count()
both accept a third argument, an options object that allows you to change how Countable treats certain aspects of your element's text.
const area = document.getElementById('area')
Countable.on(area, counter => console.log(counter), {
stripTags: true
})