Skip to Content Skip to Menu

[SOLVED] Text Area remaining character counter

  • nickr
  • nickr
  • OFFLINE
  • Posts: 49
  • Thanks: 2
  • Karma: 0
12 years 7 months ago - 12 years 5 months ago #194874 by nickr
I've searched the forum and every term I can think of on Google but have not been able to figure out the answer.

I wish to add a javascript counter to my text areas so that the remaining characters are shown. I assume that I simply embed the javascript into my index.php or reference it via a filename - that bit is fine.

I also can see that the counter should be possible using a readonly field using a field delimiter.

The part I can't figure out is how to add the onKeyDown and onKeyUp commands to my text areas.

Thanks in advance.
Last edit: 12 years 5 months ago by krileon.

Please Log in or Create an account to join the conversation.

  • krileon
  • krileon
  • ONLINE
  • Posts: 48479
  • Thanks: 8282
  • Karma: 1443
12 years 7 months ago #194896 by krileon
Replied by krileon on topic Re: Text Area remaining character counter
You'll need to add jQuery to the page to handle this. Best I can suggest is use CB Auto Actions with a Code action and Mode set to jQuery. You'd then just supply your jQuery directly in it and it'd handle the loading of CBs jQuery library for you. You'll probably want to use the before profile edit display and before registration form display triggers so it loads on those respective pages. For information on jQuery usage please see jQuery documentation below.

docs.jquery.com/Main_Page


Kyle (Krileon)
Community Builder Team Member
Before posting on forums: Read FAQ thoroughly + Read our Documentation + Search the forums
CB links: Documentation - Localization - CB Quickstart - CB Paid Subscriptions - Add-Ons - Forge
--
If you are a Professional, Developer, or CB Paid Subscriptions subscriber and have a support issue please always post in your respective support forums for best results!
--
If I've missed your support post with a delay of 3 days or greater and are a Professional, Developer, or CBSubs subscriber please send me a private message with your thread and will reply when possible!
--
Please note I am available Monday - Friday from 8:00 AM CST to 4:00 PM CST. I am away on weekends (Saturday and Sunday) and if I've missed your post on or before a weekend after business hours please wait for the next following business day (Monday) and will get to your issue as soon as possible, thank you.
--
My role here is to provide guidance and assistance. I cannot provide custom code for each custom requirement. Please do not inquire me about custom development.

Please Log in or Create an account to join the conversation.

  • nickr
  • nickr
  • OFFLINE
  • Posts: 49
  • Thanks: 2
  • Karma: 0
12 years 5 months ago #200027 by nickr
Replied by nickr on topic Re: Text Area remaining character counter
Thanks for the response. I had to put this on hold for a while but have just been trying to get it to work.

I've done as you said and have set up a CB Auto Actions in the way you mentioned. I found a jQuery script that meets my needs and inserted it in this action item. I then created another CB Auto Action in JS mode and inserted the script that went along with it as follows:

$(document).ready(function(){$('#cb_tasterterms').jqEasyCounter();});

where #cb_tasterterms is the ID of the field I wanted the Character Counter on. However, using Chrome developer tools I keep seeing the following in the Console:

Uncaught TypeError: Cannot call method 'jqEasyCounter' of null

which my research determined means that the variable for the field ID must not be correct (even though it is the correct ID).

I know this is slightly going outside of the bounds of CB but I hope that you might be able to point me in the right direction and that this might be useful for other users also.

Thanks.

Please Log in or Create an account to join the conversation.

  • krileon
  • krileon
  • ONLINE
  • Posts: 48479
  • Thanks: 8282
  • Karma: 1443
12 years 5 months ago #200031 by krileon
Replied by krileon on topic Re: Text Area remaining character counter
Code:
$(document).ready(function(){$('#cb_tasterterms').jqEasyCounter();});
You don't need to prepare the document, CB does that for you. You'd do just the below.
Code:
$( '#cb_tasterterms' ).jqEasyCounter();

Uncaught TypeError: Cannot call method 'jqEasyCounter' of null

The jQuery function jqEasyCounter you're trying to call doesn't exist in CBs scope. CB runs in a deep noConflict and can't access functions outside of it self.

I know this is slightly going outside of the bounds of CB but I hope that you might be able to point me in the right direction and that this might be useful for other users also.

You can review GJs Wall integration where it has jQuery to do input counting without a jQuery plugin or special function.


Kyle (Krileon)
Community Builder Team Member
Before posting on forums: Read FAQ thoroughly + Read our Documentation + Search the forums
CB links: Documentation - Localization - CB Quickstart - CB Paid Subscriptions - Add-Ons - Forge
--
If you are a Professional, Developer, or CB Paid Subscriptions subscriber and have a support issue please always post in your respective support forums for best results!
--
If I've missed your support post with a delay of 3 days or greater and are a Professional, Developer, or CBSubs subscriber please send me a private message with your thread and will reply when possible!
--
Please note I am available Monday - Friday from 8:00 AM CST to 4:00 PM CST. I am away on weekends (Saturday and Sunday) and if I've missed your post on or before a weekend after business hours please wait for the next following business day (Monday) and will get to your issue as soon as possible, thank you.
--
My role here is to provide guidance and assistance. I cannot provide custom code for each custom requirement. Please do not inquire me about custom development.

Please Log in or Create an account to join the conversation.

  • nickr
  • nickr
  • OFFLINE
  • Posts: 49
  • Thanks: 2
  • Karma: 0
12 years 5 months ago #200075 by nickr
Replied by nickr on topic Re: Text Area remaining character counter
Thanks for the points. In the end I've managed to do the following:

Use Auto Actions as instructed with the following code:

var characterLimit = 1000;


$('#remainingCharacters').html(characterLimit);

$('#myTextarea').bind('keyup', function(){
var charactersUsed = $(this).val().length;

if(charactersUsed > characterLimit){
charactersUsed = characterLimit;
$(this).val($(this).val().substr(0, characterLimit));
$(this).scrollTop($(this)[0].scrollHeight);
}

var charactersRemaining = characterLimit - charactersUsed;

$('#remainingCharacters').html(charactersRemaining);
});


Additionally I add a Fields Delimiter with the following in:

<span id="remainingCharacters"></span> characters remaining.


And that's it. The number of characters remaining are displayed and this code prevents the user from entering more than the defined number of characters.

Only issue I now have is that I will have to reproduce this multiple times for multiple fields which seems a bit messy so if you have any thoughts on how to do this better then appreciated.

Thanks again for your help so far.

Please Log in or Create an account to join the conversation.

  • krileon
  • krileon
  • ONLINE
  • Posts: 48479
  • Thanks: 8282
  • Karma: 1443
12 years 5 months ago - 12 years 5 months ago #200084 by krileon
Replied by krileon on topic Re: Text Area remaining character counter

Only issue I now have is that I will have to reproduce this multiple times for multiple fields which seems a bit messy so if you have any thoughts on how to do this better then appreciated.

If you adjust the selectors and append the counter directly to the fields container you can have 1 block of code that effects an unlimited amount of fields. There's no need for duplicate. Example as follows.
Code:
var selectors = $( '#myTextarea1,#myTextarea2,#myTextarea3' ); var characterLimit = 1000; selectors.append( '<div id="remainingCharacters">' + characterLimit + '</div>' ); selectors.bind( 'keyup', function() { var charactersUsed = $ (this ).val().length; if ( charactersUsed > characterLimit ){ charactersUsed = characterLimit; $( this ).val( $(this).val().substr( 0, characterLimit ) ); $( this ).scrollTop( $( this )[0].scrollHeight ); } var charactersRemaining = ( characterLimit - charactersUsed ); $( this ).find( '#remainingCharacters' ).html( charactersRemaining ); });

I didn't test it so you'll likely need to make various adjustments.


Kyle (Krileon)
Community Builder Team Member
Before posting on forums: Read FAQ thoroughly + Read our Documentation + Search the forums
CB links: Documentation - Localization - CB Quickstart - CB Paid Subscriptions - Add-Ons - Forge
--
If you are a Professional, Developer, or CB Paid Subscriptions subscriber and have a support issue please always post in your respective support forums for best results!
--
If I've missed your support post with a delay of 3 days or greater and are a Professional, Developer, or CBSubs subscriber please send me a private message with your thread and will reply when possible!
--
Please note I am available Monday - Friday from 8:00 AM CST to 4:00 PM CST. I am away on weekends (Saturday and Sunday) and if I've missed your post on or before a weekend after business hours please wait for the next following business day (Monday) and will get to your issue as soon as possible, thank you.
--
My role here is to provide guidance and assistance. I cannot provide custom code for each custom requirement. Please do not inquire me about custom development.
Last edit: 12 years 5 months ago by krileon.

Please Log in or Create an account to join the conversation.

Moderators: beatnantkrileon
Powered by Kunena Forum