Click here to Skip to main content
15,893,381 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to display the word count while giving the text in ck editor only but with my above code i am able to get the word count on button click.How to display the word count directly while giving text in ck editor instead of button click


<div>
       <ckeditor:ckeditorcontrol id="CKEditor1" basepath="/ckeditor/" runat="server"></ckeditor:ckeditorcontrol>
       <asp:button id="btn1" runat="server" onclick="btn1_Click" text="get word count" />
       <asp:label id="lbl1" runat="server"></asp:label>
   </div>


protected void Page_Load(object sender, EventArgs e)
       {
       }

       protected void btn1_Click(object sender, EventArgs e)
       {
           string whole_text = CKEditor1.Text;
           string trimmed_text = whole_text.Trim();

           // new line split here
           string[] lines = trimmed_text.Split(Environment.NewLine.ToCharArray());

           // don't need this here now...
           //string[] split_text = trimmed_text.Split(' ');

           int space_count = 0;
           string new_text = "";
           foreach (string line in lines)
           {
               // Modify the inner foreach to do the split on ' ' here
               // instead of split_text
               foreach (string av in line.Split(' '))
               {
                   if (av == "")
                   {
                       space_count++;
                   }
                   else
                   {
                       new_text = new_text + av + ",";
                   }
               }
           }

           new_text = new_text.TrimEnd(',');

           // use lines here instead of split_text
           lines = new_text.Split(',');
           //MessageBox.Show(lines.Length.ToString());
           lbl1.Text = lines.Length.ToString();
       }


What I have tried:

my above code gives the word count on button click instead i need to get the word count while typing the text in ckeditor itself.How can i do this
Posted
Updated 3-Feb-17 1:23am

As you can see, your current code is written in C#, so it's running on the server side - what you are going to have to do is rewrite your logic in JavaScript and then bind it in to the change logic using the ck event listener logic. You can find an example of listening to the onChange event here[^].
 
Share this answer
 
Comments
Member 12324523 3-Feb-17 7:20am    
I am unable to do that only also this link is not accessible to me.Can anyone help me out as i am new to this
Pete O'Hanlon 3-Feb-17 8:59am    
Nobody is going to write the JavaScript you need out for you. This isn't rentacoder. If you take a little bit of time with it you'll learn the basics of a new language and you'll have a better idea as to how to work on the client side.
Hi,

use textchange event of CKEditor1. ontextchange event write the same logic which you have written in button_click event
 
Share this answer
 
Comments
Member 12324523 3-Feb-17 7:57am    
I wrote but its not firing and not working

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900