1

Topic: Noob question: char counter

Hi all.

I have just discovered Tiny MCE and am really impressed: kudos to the developers.

I'm a bit confused as the forums description states "ask how to do various things here" but a sticky clearly says "DO NOT submit bug reports or feature requests here".

I'm not sure if this is a feature request, if so accept my apologies and feel free to move to the appropriate place. big_smile

I'm wondering if there's a way (plugin or something) to display a character counter and to limit the maximum number of characters allowed in each textarea.

I tried a quick search, but couldn't find anything: this surprised me a bit as I thought it was a common requirement. Did I miss something obvious?

TIA.

Alessandro

2

Re: Noob question: char counter

Well, it's 100% ok to discuss a idea here. But when it comes to requests like "I would like TinyMCE to do this and that" it's better to submit a feature request at Sourceforge.

The big problem with a character counter for TinyMCE is that WYSIWYG editor contains much more than characters, since it contains HTML markup tags. So what should it count, the number of real characters including <strong> or just characters that are displayed to the user? Or both, and whats the purpose of the count, is it to check the max length of a database post or the visual area. If it's a visual area it should check the pixel width/height not the char count.

Best regards,
Spocke - Main developer of TinyMCE

3

Re: Noob question: char counter

Thanks for your reply.

whats the purpose of the count, is it to check the max length of a database post or the visual area.

The former: I'm posting my form data to one db table. Of course there will be some check server side, but it would be nice to have some basic check also on the client. Therefore I'd like to have a count of the characters that are going to be actually POSTed: ideally it should be a counter dynamically displaying the remaining characters "as I type", but also a check before form submit could do, if that's easier to implement.

Regards.

Alessandro

4

Re: Noob question: char counter

Then you could use tinyMCE.getContent('<editor id or element name/id>').length to get the HTML content length in chars. Place this in a script on onsubmit or simmilar and just alert out "To many chars bla bla".

Best regards,
Spocke - Main developer of TinyMCE

5

Re: Noob question: char counter

I've almost completed a Word/Character counter plugin, will post it up tomorrow or something. Works like the Word count in MS Word.

Ryan.

just because you're not paranoid, doesn't mean everybody isn't out to get you.

6

Re: Noob question: char counter

Has this plugin been developed yet?

Any idea if there is something similar available to use?

Thanks

7

Re: Noob question: char counter

Hi, is this developed plugin as download available ?

8

Re: Noob question: char counter

Hi there,

I'd quite like to dynamically display the number of characters that are being shown (so excluding any html).

I'm new to TinyMCE, but it looks like using tinyMCE.getContent('<editor id or element name/id>').length, on it's own is useful when processing the info.
Could anyone tell me where to put this code (or other) to access this info as it's being typed?

I see from the posts that "ssdesign" was developing a plugin.  I can't see it in the list.  That would be brilliant. Any news?

Many thanks in advance.

9

Re: Noob question: char counter

The following worked for me it restricts the user by limiting character to 450

tinyMCE.init({
                mode : "exact",
                elements : "description",
                theme : "simple",
                setup : function(ed) {
                ed.onKeyUp.add(function(ed, e) {
                    txt = tinyMCE.activeEditor.getContent();
                    //strip the html
                    txt = txt.replace(/(<([^>]+)>)/ig,"");
                    txt = txt.replace(/¬†/g,"");
                    if (txt.length > 450) {
                          txt = txt.substring(0,450);
                          tinyMCE.execCommand('mceSetContent',false,txt);
                  }
                });
             }
          });

10

Re: Noob question: char counter

Hi junaid,

your post is worthless. The last post in this thread is over half a year old. The TinyMCE version in use probably is the 2.x branch (RC2 of the 3.x branch got out in January 2008!!!) so your code probably doesn't fit anyways.

Greetings from Germany,

Felix Riesterer.
(-> about me and this forum <-)

11

Re: Noob question: char counter

I wrote the code to do a word and char count as you type in tinyMCE:
It shows in the tinyMCE status bar.

tinyMCE.init({
    ....

     //Character count
     theme_advanced_path : false,
     setup : function(ed) {
          ed.onKeyUp.add(function(ed, e) {   
               var strip = (tinyMCE.activeEditor.getContent()).replace(/(<([^>]+)>)/ig,"");
               var text = strip.split(' ').length + " Words, " +  strip.length + " Characters"
        tinymce.DOM.setHTML(tinymce.DOM.get(tinyMCE.activeEditor.id + '_path_row'), text);   
    });
     }

});

12

Re: Noob question: char counter

Hi All,
I think we don't need to use reg-exp to remove tags and count characters, not sure how fast the regexp implementation, but i believe that the way I use should work faster and precisely

here is the way to do it via DOM

/**
* Return the "clean-text" length of the edited text in the tinyMCE
* @param editorId
*/
function getLengthMCE(editorId) {

    // Get the editor instance that we want to interact with.
    var oEditor = tinyMCE.get(editorId) ;

    // Get the Editor Area DOM (Document object).
    var oDOM = oEditor.getDoc() ;

    var iLength ;
    // The are two diffent ways to get the text (without HTML markups).
    // It is browser specific.
    if (document.all) {
        // If Internet Explorer.
        iLength = oDOM.body.innerText.length;
    } else {
        // If Gecko.
        var r = oDOM.createRange() ;
        r.selectNodeContents(oDOM.body);
        iLength = r.toString().length;
    }

    return iLength;
}


Andrey Chorniy

13

Re: Noob question: char counter

hi, i need to implement character count and would like to use the function Andreaz wrote.

can someone please quickly direct me to where actually should i put it in (which part of the code) and how to enable it?

i have no experience with the tinymce code and it would save me a lot of trouble if i could just get a quick guidance..

thanks for your help!
gigi

14

Re: Noob question: char counter

Search the forum about this issue. It has been dealt with endlessly...

Greetings from Germany,

Felix Riesterer.
(-> about me and this forum <-)

15

Re: Noob question: char counter

Good idea. thanks very much.