Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ countOverflowText : "Maximum %type exceeded by %d", // count overflow
countOverflowContainerClass : "text-count-overflow-wrapper", // class applied to the count overflow wrapper
minDisplayCutoff : -1, // maximum number of characters/words above the minimum to display a count
maxDisplayCutoff : -1, // maximum number of characters/words below the maximum to display a count

counterId' : null, // custom ID for the counter element (e.g. to prevent conflicts). If one is not provided, a default ID will be assigned based on the ID of the element.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
counterId' : null, // custom ID for the counter element (e.g. to prevent conflicts). If one is not provided, a default ID will be assigned based on the ID of the element.
counterId : null, // custom ID for the counter element (e.g. to prevent conflicts). If one is not provided, a default ID will be assigned based on the ID of the element.

// Callback API
maxunder : function(el){}, // Callback: function(element) - Fires when counter is under max limit
minunder : function(el){}, // Callback: function(element) - Fires when counter is under min limit
Expand Down
11 changes: 9 additions & 2 deletions textcounter.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@
base.init = function() {
base.options = $.extend({}, $.textcounter.defaultOptions, options);

var counterElementId = base.options.counterId ?? base.el.id + '_counter';
Copy link
Owner

@ractoon ractoon Sep 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My only concern with setting it this way is if there are multiple instances on a page, with an empty ID, then this would create duplicate IDs. Perhaps something like jQuery UIs uniqueId function behavior could be duplicated here instead: https://github.com/jquery/jquery-ui/blob/1be45388176080418aef84f3c98fda2c10fcdc42/ui/unique-id.js#L43


// append the count element
var counterText = base.options.countDown ? base.options.countDownText : base.options.counterText,
counterNum = base.options.countDown ? base.options.max : 0,
$formatted_counter_text = $('<div/>').addClass(base.options.textCountMessageClass)
.attr('aria-live', 'polite').attr('aria-atomic', 'true')
.html(counterText.replace('%d', '<span class="' + base.options.textCountClass + '">' + counterNum + '</span>')),
.attr('aria-live', 'polite').attr('aria-atomic', 'true').attr('id', counterElementId)
.html(counterText.replace('%d', '<span class="' + base.options.textCountClass + '" role="text">' + counterNum + '</span>')),
$count_overflow_text = $('<div/>').addClass(base.options.countOverflowContainerClass);

base.hideMessage($count_overflow_text);
Expand All @@ -39,6 +41,10 @@
base.$text_counter = base.$container.find('span');
base.$el.after(base.$container);

var curDescribedBy = base.$el.attr('aria-describedby') ?? '';
var describedByWithCount = (curDescribedBy + ' ' + counterElementId).trim();
base.$el.attr('aria-describedby', describedByWithCount);

// bind input events
base.$el.bind('keyup.textcounter click.textcounter blur.textcounter focus.textcounter change.textcounter paste.textcounter', base.checkLimits).trigger('click.textcounter');

Expand Down Expand Up @@ -340,6 +346,7 @@
'countOverflowContainerClass' : "text-count-overflow-wrapper", // class applied to the count overflow wrapper
'minDisplayCutoff' : -1, // maximum number of characters/words above the minimum to display a count
'maxDisplayCutoff' : -1, // maximum number of characters/words below the maximum to display a count
'counterId' : null, // custom ID for the counter element (e.g. to prevent conflicts). If one is not provided, a default ID will be assigned based on the ID of the element.

// Callback API
'maxunder' : function(el){}, // Callback: function(element) - Fires when counter under max limit
Expand Down
2 changes: 1 addition & 1 deletion textcounter.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.