troubleshooting Question

JQuery to count number of characters in multiline text box doesn't run after update panel postback

Avatar of iftech
iftech asked on
JavaScriptASP.NETjQuery
12 Comments1 Solution1138 ViewsLast Modified:
Hi All,

I asked a question similar to this before and got a working solution. However It doesn't seen to work here.
I have a multi-line text in an update panel (the only one on the page). The J query counts the characters in the text box up to a max 50 and updates a label with the char count.
After 50 chars it doesn't allow anymore input into the text-box. This all works fine, until a post-back occurs caused by a drop-down in the same update panel. Then the label shows 50 out of 50 characters remaining and does not update after any more input even if the chars in the text-box are less than 50.
Here is the script:
<script type="text/javascript" language="javascript" >
    $(function() {
        var limit = 50;
        var tb = $('textarea[id$=txtMessage]');
        $(tb).keydown(function() {
            var len = $(this).val().length;
            if (len > limit) {
                this.value = this.value.substring(0, 50);
                len = 50;                    
            }
            $('#spnCharCount').text("Characters remaining " + (limit - len) + "/" + limit);
        });
    });
   
     // add anonymous function to the page_loaded event;
     // every time a partial postback occurs, the page_loaded is being executed and statements added to this event handler will execute
     Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(function() {
       $('textarea[id$=txtMessage]').keydown();
     });
</script>

Thanks
John.
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 12 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 12 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros