Socket
Socket
Sign inDemoInstall

alpinejs-character-count

Package Overview
Dependencies
0
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    alpinejs-character-count

Track the character count of an input with the option to compare against the max length 🔟


Version published
Weekly downloads
75
increased by13.64%
Maintainers
1
Install size
5.29 kB
Created
Weekly downloads
 

Readme

Source

Alpine JS Character Count

Track the character count of an input with the option to compare against the max length 🔟

Example

<div x-data="{ message: 'Hello' }">
  <textarea x-model="message" maxlength="50"></textarea>

  <p>You have written <span x-count="message"></span> characters.</p>

  <p>You have <span x-count.50="message"></span> characters remaining.</p>
</div>

This will return the following:

<p>You have written 5 characters.</p> <p>You have 45 characters remaining</p>

The modifier in the example 50 is the max character count which you can pair up with the maxlength attribute on the HTML input. If you want a more dynamic approach to this, you can do the following.

Using Alpine JS Refs

<div x-data="{ message: 'Hello' }">
  <textarea x-model="message" maxlength="100" x-ref="textarea"></textarea>

  <p>You have written <span x-count="message"></span> characters.</p>

  <p>You have <span x-count.textarea="message"></span> characters remaining.</p>
</div>

Instead of passing the max character count as the modifier, you pass the name of an Alpine JS x-ref.

Install

CDN

<script
  defer
  src="https://unpkg.com/alpinejs-character-count@latest/dist/count.min.js"
></script>

<script defer src="https://unpkg.com/alpinejs@3.x.x/dist/cdn.min.js"></script>

NPM/Yarn

npm i -D alpinejs-character-count

yarn add -D alpinejs-character-count

Then you can register the plugin.

import Alpine from 'alpinejs'
import count from 'alpinejs-character-count'

Alpine.plugin(count)

window.Alpine = Alpine

Alpine.start()

Stats

Keywords

FAQs

Last updated on 04 Apr 2024

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc