Chris Hopkins
Chris Hopkins

Reputation: 23

jQuery HtmlBox limit number of words/chars entered

I have HtmlBox 4.0.3 from Remiya on a Joomla website and would like to know how (if possible) I would be able to limit the amount of chars/words entered into the textarea of HtmlBox.

I've already managed to limit the chars/words on a plain html textarea but HtmlBox is written completely in jQuery and I don't even know where to start.

Any help much appreciated,

Chris

Upvotes: 1

Views: 305

Answers (1)

billyonecan
billyonecan

Reputation: 20260

Here's a really basic example of what you could do:-

$(document).ready(function() {
  var maxLength = 150; // max number of allowed characters
  $('#yourHtmlBoxId').keyup(function() {
    $(this).val($(this).val().substr(0, maxLength));
    $('#charCount').text(maxLength - $(this).val().length);
  });
  $('#yourHtmlBoxId').trigger('keyup');
});

Obviously you need to replace #yourHtmlBoxId and #charCount with the relevant selectors.

Some example markup:

<textarea id="yourHtmlBoxId"></textarea>
<p><span id="charCount"></span> remaining</p>

Here's a fiddle for you to play with

Just bear in mind that this is by no means bullet proof, it works on the keyup event, so for example if somebody copy/pasted into the textarea via mouse buttons the jQuery would never be triggered. This should point you in the right direction though.

Upvotes: 1

Related Questions