Skip to content

Instantly share code, notes, and snippets.

@aseroff
Created June 22, 2023 17:26
Show Gist options
  • Save aseroff/e69020e283db86d86e03946e628c3482 to your computer and use it in GitHub Desktop.
Save aseroff/e69020e283db86d86e03946e628c3482 to your computer and use it in GitHub Desktop.
text with character limit html & stimulus controller
%div{data: {controller: 'text-with-limit'}}
- limit = 1000
%label Message:
= form.text_area :custom_message, value: default_message, maxlength: limit, rows: 9, data: { 'text-with-limit-target': 'field', action: 'keyup->text-with-limit#updateCounter' }
.right
%small
Chars left:
%span{data: {'text-with-limit-target': 'counter'}}= limit - default_message.length
\/
= limit
import { Controller } from '@hotwired/stimulus'
export default class extends Controller {
static targets = [ 'field', 'counter' ]
updateCounter() {
this.counterTarget.textContent = this.fieldTarget.maxLength - this.fieldTarget.value.length
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment