Paste the following function code at the page level (Function and Global Variable Declaration), or create a region in the Global Page using the Blank template. In that region, select Source and paste the function code enclosed within <script></script> tags.
function setPostTextCounter(itemId)
{
var item = apex.item(itemId);
var inputEl = item.element;
var maxLen = inputEl.attr("maxlength") || 100;
var curLen = item.getValue().length;
var counterId = itemId + "_counter";
var postText = inputEl.siblings(".t-Form-itemText--post");
if (postText.length === 0) {
postText = $('<span class="t-Form-itemText t-Form-itemText--post"></span>');
inputEl.after(postText);
}
if (postText.find("#" + counterId).length === 0) {
postText.append('<span id="' + counterId + '" class="apex-char-counter"></span>');
}
var counter = $("#" + counterId);
counter.text(curLen + " / " + maxLen);
counter.removeClass("is-warning is-danger");
if (curLen >= maxLen) {
counter.addClass("is-danger");
} else if (curLen >= maxLen - 2) {
counter.addClass("is-warning");
}
}
Similarly, paste the following CSS code at the page level (inline CSS), or create a region in the Global Page using the Blank template. In that region, paste the CSS code inside <style></style> tags.
.apex-char-counter
{
font-size: 11px;
color: #6a6a6a;
margin-left: 6px;
}
.apex-char-counter.is-warning
{
color: orange;
}
.apex-char-counter.is-danger
{
color: red;
font-weight: 600;
}
Now, in our application, we can call this function on any page by defining it in the Global Page.
Create a page item with the Text Field type and set the Maximum Length value, because the JavaScript function reads the maximum length from this attribute.
Create an Input Dynamic Action on the required page item, and in the True Action, call the function.
setPostTextCounter("P19_PHONE");
Comments
Post a Comment