css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

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. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation


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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

              <div class="info">
  <h1>Custom <code>word-count</code> element</h1>
  <p>This custome element counts the words typed into a textarea, input element or div with <code>contenteditable</code> attribute</p>
  <p><b>BROWSER SUPPORT:</b>Works native in Chrome, Safari, and Opera.</p>
<div class="demo-area">
<div class="box">
  <h2>Bound to <code>&lt;textarea></code></h2>
<textarea name="" id="my-box" cols="50" rows="10" placeholder="Enter some text"></textarea>
<word-count bind="#my-box" delay="100"></word-count>

<div class="box">
  <h2>Bound to <code>&lt;input></code></h2>
<input type="text" name="" id="my-input"placeholder="Enter some text" />
<word-count bind="#my-input" delay="100"></word-count>

<div class="box">
  <h2>Bound to <code>&lt;div contenteditable></code></h2>
  <div contenteditable id="my-div">Enter some text</div>
<word-count bind="#my-div" delay="100" limit="5"></word-count>
              * {
  margin: 0;
  padding: 0;
  box-sizing: border-box;

body {
  font: 16px/1.5 Arial, Helvetica, sans-serif;

.info {
  width: 100%;
  text-align: center; 
  border-bottom: 1px solid #aaa;
  padding: .5rem 0;
  line-height: 1.8;

code {
  background: #eee;
  padding: 0 .125em;

.demo-area {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100vh;

.box {
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;

.box + .box {
  margin-left: 4rem;
  padding-left: 4rem;
  border-left: 2px solid #aaa;
  height: 100%;

textarea, input, div[contenteditable] {
  width: 100%;
  background: #ddd;
  color: #333;
  font-size: 1.3rem;
  padding: .5rem;
  border-radius: 3px 3px  0 0;
  border: 1px solid #aaa;
  border-bottom: none;

word-count {
  display: block;
  background: #ddd;
  width: 100%;
  border-radius: 0 0 3px 3px;
  border: 1px solid #aaa;
  border-top: 1px solid #ccc;
  padding: .25rem;
  text-align: right;

              // Web Components are created by directly extending the HTMLElement 
// which is the base class for all HTML Elements (<p>, <h1>, <div>, etc.)
class WordCount extends HTMLElement {
  constructor() {
    // Web Component setup
    this.attachShadow({mode: "open"})
    // Initialize a base state
    this.state = {
      count: 0,
      limitReached: false
    // Get the element to observe
    this._boundEl = document.querySelector(this.getAttribute('bind'))
    // Init an empty timeput
    this._timeout = null;
    // Get the delay, defaults to 500ms
    this._delay = parseInt(this.getAttribute('delay')) || 500;
    // A optional character limit
    this._limit = parseInt(this.getAttribute('limit')) || undefined;
    // If there is a bound element, attach an event listener
    if (this._boundEl) {
      this._boundEl.addEventListener('keyup', (e) => {
        this._timeout = setTimeout( () => {
          let count = 0;
          // Check if e.target.value is defined 
          if (!!e.target.value) {
            count = e.target.value.split(' ').length;
          } else {
            // If there is no e.target.value try and read the textContent
            count = e.target.textContent.split(' ').length;
          // Update the word count
          if(this._limit != undefined) {
        }, this._delay)
  CSS() {
    return `
    .limit-reached {
      color: var(--word-count--limit-color, red);
  updateLimitReached(val) {
    this.state.limitReached = parseInt(val) >= parseInt(this._limit) 
  updateCount(val) {
    // Update the state
    this.state.count = val;
    // Re-render the component
  render(state) {
    let className = this.state.limitReached ? 'limit-reached' : '';
    // Update the innerHTML of the Shadow DOM
    this.shadowRoot.innerHTML = `${this.CSS()}<p class="${className}">You typed ${state.count} words</p>`;

// Register the custom element. All <word-count> HTML elements will be constructed 
// by using the WordCount Class defined above.
window.customElements.define('word-count', WordCount);
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................