Git Product home page Git Product logo

ds-character-count's Introduction

Character count

This is a prototype. We don't recommend using it in production.

Introduction

Character count is a component that:

  • helps users provide information in a limited number of characters
  • tells users how many characters they have remaining by counting down as they type
  • provides feedback for assistive technology so that all users are aware of the character limit
  • shows the number of remaining words or characters

This component will be part of the govuk-frontend, a single package containing everything needed to start building a GOV.UK service.

Demo

[Screenshot]

Guidance

Requirements

This component prototype has been built using govuk_prototype_kit.

Dependencies:

Installation

Usage

  1. Set the maxlength attribute and add a javascript handler class or id (e.g. .js-char-count). Note that the textarea must have an id attribute set to a unique value within the page.
<textarea id="description-field" class="form-control js-char-count" rows="5" maxlength="500"></textarea>
  1. Enhance the textarea by initialising a charCount instance on the specified handler (e.g. .js-char-count)
var charCount = new GOVUK.CharCount()
charCount.init({
  selector: '.js-char-count'
})

Variants

Word count

  1. Since there is no maxwords attribute in HTML5, use data-maxwords attribute to set the maximum number of words and add a javascript handler class (e.g. .js-word-count). Note that the textarea must have an id attribute set to a unique value within the page.
<input id="name-field" class="form-control js-word-count" type="text" data-maxwords="10">
<textarea id="description-field" class="form-control js-word-count" rows="5" data-maxwords="30"></textarea>
  1. Enhance the textarea by initialising a charCount instance on the specified handler (e.g. .js-word-count) to display word count. For initialising the Word Count you must add wordCount: true to the options of the init function.
var charCount = new GOVUK.CharCount()
charCount.init({
  selector: '.js-word-count',
  wordCount: true
})

Character count with threshold

You can display the count message when the users are at 50% or more setting a percentage value as threshold.

var charCount = new GOVUK.CharCount()
charCount.init({
  selector: '.js-char-count',
  wordCount: true,
  threshold: 50
})

Character count with highlight

You can also visually highlight the characters which have taken users over the limit by setting the highlight option to true. This can make it easier for some users to see how far over they are and edit their content to fit. However, for some lower confidence, it can cause confusion.

var charCount = new GOVUK.CharCount()
charCount.init({
  selector: '.js-char-count',
  highlight: true
})

Accessibility

The acceptance criteria describes the necessary behaviours a character count needs to meet to be usable by assistive technologies.

ds-character-count's People

Stargazers

 avatar

Watchers

 avatar  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.