Giter Club home page Giter Club logo

countable's Introduction

Countable

Countable is a JavaScript function to add live paragraph-, word- and character-counting to an HTML element. Countable does not rely on any libraries and stays under 1KB when minified and gzipped.

View the Demo

Installation

Usage

Countable enables live counting on <input> and <textarea> elements as well as elements that have the contenteditable attribute. Elements whose text contents cannot be edited are simply parsed once.

You can define your own callback function that Countable should use. If you don't pass such a function, the results are simply logged to the console.

<script src="js/Countable.js"></script>
<script>
  var elem = document.getElementById('field')

  new Countable(elem, function (counter) {
    alert(counter.words)
  })
</script>

Countable takes the value from an HTML element and counts paragraphs, words and characters (with and without spaces). The callback function can receive a single parameter. This parameter is an object containing all the calculated values. In the above example, counter holds all numbers returned from Countable.

counter = {
  paragraphs: 0,
  words: 0,
  characters: 0,
  all: 0
}
Property Meaning
paragraphs The number of paragraphs. Paragraphs can be separated by either a soft or a hard (two line breaks) return. To use hard returns, add a truthy parameter to your Countable call after the callback.
words The number of words. Words are split using spaces.
characters The number of characters (without spaces). This contains all non-whitespace characters.
all The number of characters including whitespace. This is the total number of all characters in the element.

You can optionally change Countable's behaviour by passing an options object as the third parameter.

Option Value Meaning
hardReturns Boolean Use two returns to seperate a paragraph instead of one.
stripTags Boolean Strip HTML tags before counting the values.

Browser Support

Chrome Safari Firefox Opera Internet Explorer
Yes Yes Yes Yes 7+

About the Author

My name is Sacha Schmid. I'm a front-end engineer from Switzerland. I am the creator of SSGS and other open source projects.

Contributors

countable's People

Contributors

bensmithett avatar epmatsw avatar radlikewhoa avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.