Click here to Skip to main content
15,893,668 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
Hi there,
I m using the Keydown event of a multiline text box to call a javascript function which gets the length of the text in the text box and displays it on a label.
But when i type one letter the character count is displayed as 0 and when i type 2 letter it shows 1.
Can anyone please help me understand why it is behaving this way n how i can fix it?

The javascript function
XML
var length = document.getElementById('<%= txtMessage.ClientID %>').value.length;
         document.getElementById('<%= lblChar1.ClientID%>').innerText = length  + " characters of 160 used";


Thank You.
Ann
Posted
Comments
walterhevedeich 9-Jun-11 2:27am    
Are you using master page?
Sergey Alexandrovich Kryukov 9-Jun-11 2:43am    
It does not matter really, it's just the event handler... Please see my code sample -- it works.
--SA
Sergey Alexandrovich Kryukov 9-Jun-11 2:32am    
It's even worse than that. I'm just trying to test it and see strange behavior. My 5 for the question.
--SA
Sergey Alexandrovich Kryukov 9-Jun-11 2:41am    
Oh no, not a big deal -- I found it. Please see my code sample.
--SA

Got your problem: You are using KeyDown event which is fired when the key is pressed but till you release the key, the character doesn't become the part of the textbox. Try using KeyUp event instead.
Try below JQuery code:
$(document).ready(function() {
            $("#txtSignature").keyup(function(event) {
                alert($(this).val().length);
            });
        });
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 9-Jun-11 2:39am    
It looks like that is the problem, my 5.
I created a work-around in code and tested it on a simple sample. Please see my solution -- it works correctly.
--SA
AnnSJ 9-Jun-11 2:47am    
thanks .. thats sorted..
Now ,there are buttons which allow user to insert text into this text box. But when thathappens, the character count is not updated.
after text is added , I do call the function to recalculate the text box length and display the character count, but it stays the same. It only changes if i press a key after the text insert.
:?
AnnSJ 9-Jun-11 2:51am    
sorry.. its was silly mistake from myside.. sorted..thanks for the help.
Here is the work-around:

HTML
<html>
   <head>
      <script type="text/javascript"><!--
         function counter(eventInstance) {
            tarea = document.getElementById("tarea");
            output = document.getElementById("output");
            output.innerHTML = tarea.value.length + " typed";
         } //filterOut
      --></script>
   </head>
<body">

<textarea id="tarea" onkeyup="counter(event);" onkeydown="counter(event);"></textarea>
<div id="output" />

</body>
</html>


I tested it — it works correctly.

—SA
 
Share this answer
 
v3
Comments
AnnSJ 9-Jun-11 2:52am    
thank you. i modified my code to a logic similar to urs..
Sergey Alexandrovich Kryukov 9-Jun-11 3:10am    
I understand. Is your problem solved by now?
Thanks for accepting the answer.
Good luck, call again.
--SA
AnnSJ 9-Jun-11 3:26am    
yes...it is ..

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