This is a migrated thread and some comments may be shown as answers.

get_text().trim().length

3 Answers 112 Views
Editor
This is a migrated thread and some comments may be shown as answers.
Jason
Top achievements
Rank 1
Jason asked on 14 Nov 2008, 08:44 PM
I was wondering why the enter key is not being included in the get_text().trim().length when I press enter. Only when I type in another key (alphabets) is the enter key being taken into account.
I am trapping the onkeyup and trying to update the text count.

        function GetDraggableTextLength()
        {
            var rtfEditor = $find("<%=popupWin_txtValue.ClientID %>");
            return rtfEditor.get_text().trim().length;
        }

        function HandleKeyUp()
        {
            var latestCharCount = GetDraggableTextLength();

My QA is complaining, they press a lot of enter, they don't see the length and all of a sudden, when they press a all of a sudden they reached the maximum text length.

Thanks

3 Answers, 1 is accepted

Sort by
0
Tervel
Telerik team
answered on 17 Nov 2008, 02:48 PM
Hi Jason,

get_text returns the content of the editor as pure text, with all HTML tags stripped.
A"new line" in HTML technically means the <br> tag.... which is still a tag, and hence - it will get stripped if you use the get_text method.

In your scenario you should implement a more fine tuned method for determining the editor's content length.
Please use the get_html method - which returns the editor content as HTML.


Regards,
Tervel
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Jason
Top achievements
Rank 1
answered on 17 Nov 2008, 02:59 PM
Hi Tervel,

Thanks for the reply.

My problem is really, if you want it strip, then strip it as long as it is consistent. It is not consistent with what you are saying.

If I have this:
abc plus 2 enter keys (<br> tags), the length will return 3 which is correct based on what you are saying.
abc


But if I have this:
abc plus 2 enter keys (<br> tags) and then followed by d, the length will return 8 which should be 4 based on what you are saying.
abc

d

Why is that inconsistent? It should have stripped the br tags and not include it in the length. That is basically my question.

0
Tervel
Telerik team
answered on 24 Nov 2008, 10:08 AM
Hi Jason,

Here is some additional explanation on the behavior: when HTML tags are removed, other symbols such as the "invisible: new line character - \n or \r are kept.

In case #1 your content is reduced to "abc\r\n\r\n" - then when trim() is called, it will strip the starting/ending whitespace characters. The end result will be "abc" - and its length will be 3.

In case #2 your content is reduced to "abc\r\n\r\nd". Trimming it causes no change (as trim only removes starting/ending whitespaces) - so you end up having 8 characters.

If in your scenario you need to calculate length with new line characters removed, then you should use an additional simple regular expression that will first remove them from the content - before calculating its length.


Greetings,
Tervel
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
Editor
Asked by
Jason
Top achievements
Rank 1
Answers by
Tervel
Telerik team
Jason
Top achievements
Rank 1
Share this question
or