Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="form-group">
  <label for="char-up">Character Limit 20 - Count Up</label>
  <textarea name="char-up" rows="3" data-limit-char="20"></textarea>
</div>

<div class="form-group">
  <label for="char-down">Character Limit 20 - Count Down</label>
  <textarea name="char-down" rows="3" data-limit-char="20" data-limit-dir="down"></textarea>
</div>

<div class="form-group">
  <label for="word-up">Word Limit 8 - Count Up</label>
  <textarea name="word-up" rows="3" data-limit-word="8"></textarea>
</div>

<div class="form-group">
  <label for="word-down">Word Limit 8 - Count Down - Configured By Data Plugin Options</label>
  <textarea name="word-down" rows="3" data-plugin-options='{"mode":"word", "limit":"8", "direction":"down", "textWordDown":"words left."}'></textarea>
</div>

<div class="form-group">
  <label for="defaults">Default Plug-in Params</label>
  <input name="defaults" type="text" size="50" />
</div>
              
            
!

CSS

              
                body
  font: 14px sans-serif

.form-group
  margin-bottom: 1.5em

textarea, input
  display: block
  width: 250px

textarea
  resize: vertical

label
  color: #999
  text-transform: uppercase
  letter-spacing: 1px
  font-size: 10px

.limit-valid
  color: green
  font-weight: bold

.limit-invalid
  color: red
  font-weight: bold
              
            
!

JS

              
                /*
 *  jQuery inputLimit
 */

;(function ( $, window, document, undefined ) {
  // default options
  var pluginName = "inputLimit",
    defaults = {
      mode:           "character", // "character" or "word"
      direction:      "up",        // "up" or "down"
      limit:          50,
      displayEl:      "div",
      displayElPos:   "after",     // "before" or "after"
      displayElClass: "limit-display",
      validClass:     "limit-valid",
      invalidClass:   "limit-invalid",
      textWordUp:     "words.",
      textWordDown:   "words remaining.",
      textCharUp:     "characters.",
      textCharDown:   "characters remaining."
    };

  // constructor
  function Plugin ( element, options ) {
    this._name     = pluginName;
    this._defaults = defaults;
    this._options  = options;
    this.element   = element;
    this.element$  = $(element);
    this.metadata  = this.element$.data( 'plugin-options' );
    this.init();
  }

 Plugin.prototype = {
    init: function () {
      // extend defaults with options and metadata
      this.config = $.extend( {}, this._defaults, this._options, this.metadata );
      // create container for limit display
      this.displayEl$ = $( '<' + this.config.displayEl + '>', { 'class': this.config.displayElClass } );
      // insert limit container
      if ( this.config.displayElPos == "after" ) {
        this.displayEl$.insertAfter( this.element );
      } else if ( this.config.displayElPos == "before" ) {
        this.displayEl$.insertBefore( this.element );
      }
      // get direction
      this.config.direction = this.element$.attr( 'data-limit-dir' )
        ? this.element$.attr( 'data-limit-dir' )
        : this.config.direction;
      // get moded
      this.mode = this.element$.attr( 'data-limit-char' )
        ? "character"
        : this.element$.attr( 'data-limit-word' )
          ? "word"
          : this.config.mode;
      // get max
      this.limitMax = this.element$.attr( 'data-limit-char' ) || this.element$.attr( 'data-limit-word' ) || this.config.limit;
      // set text
      this.updateLimitDisplay();
      // bind events
      this.bindEvents();
    },
    
    bindEvents: function() {
      // browser support for oninput
      var supportOnInput = 'oninput' in document.createElement('input');
      // when the limit event fires, update the display
      this.element$.on( supportOnInput ? "input.limit" : "keyup.limit", this.updateLimitDisplay.bind(this) );
    },
    
    updateLimitDisplay: function() {
      this.displayEl$.text( this.getText() );
    },
    
    getCount: function() {
      var count = 0,
          val = this.element$.val();
      
      // if limit mode is "character"
      if ( this.mode == "character" ) {
        // if the direction is up then the count is the input value length
        // if the direction is down then the count is the max length - the current length
        count = this.config.direction == "up"
          ? val.length
          : this.limitMax - val.length;
        
      // if limit mode is "word"
      } else if ( this.mode == "word" ) {
        
        // split text value at spaces and remove empty strings, then count words
        var words = val.split(' ').filter(function (a) {
          return a === "" ? 0 : a;
        }).length;
        
        // if the direction is up then the count is the number of words
        // if the direction is down then the count is the max length - the number of words
        count = this.config.direction == "up"
          ? words
          : this.limitMax - words;
      }
      
      // check if count exceeds allowed amount
      this.validateCount( count );

      return count;
    },
    
    getText: function() {
      // begin text with count
      var text = this.getCount() + ' of ' + this.limitMax + ' ';
            
      // if limit by character and direction is up
      if ( this.mode == "character" ) {
        return text + this.config[ this.config.direction == "up" ? "textCharUp" : "textCharDown" ];
      }
      
      // if limit by word and direction is up
      if ( this.mode == "word" ) {
        return text + this.config[ this.config.direction == "up" ? "textWordUp" : "textWordDown" ];
      }
    },
    
    validateCount: function( count ) {
      if (
        // direction is up and count exceeds max OR
        // direction is down and count is less than 0
        ( this.config.direction == "up"   && count > this.limitMax ) ||
        ( this.config.direction == "down" && count < 0 ) ) {
        
        // invalidate
        this.displayEl$
          .removeClass( this.config.validClass )
          .addClass( this.config.invalidClass );
        
      } else if (
        // direction is up and count is greater than 0 and less than or equal to max OR
        // direction is down and count is greater than or equal to 0 and not equal to max
        ( this.config.direction == "up"   && count > 0  && count <= this.limitMax ) || 
        ( this.config.direction == "down" && count >= 0 && count != this.limitMax ) ) {
        
        // validate
        this.displayEl$
          .removeClass( this.config.invalidClass )
          .addClass( this.config.validClass );           
      }
    }
  };
  

  // prevent against multiple instantiations
  $.fn[ pluginName ] = function ( options ) {
    this.each(function() {
      if ( !$.data( this, "plugin_" + pluginName ) ) {
        $.data( this, "plugin_" + pluginName, new Plugin( this, options ) );
      }
    });

    // chain jQuery functions
    return this;
  };
})( jQuery, window, document );
 

// instantiate
$('textarea, input').inputLimit();
              
            
!
999px

Console