Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7181875
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:38:58+00:00 2026-05-28T17:38:58+00:00

I’m using jQuery to display a counter above a textbox of how many characters

  • 0

I’m using jQuery to display a counter above a textbox of how many characters the user has entered and how many they have left. It does this by updating the .text() of a span above the textbox.

So in $(document).ready I initially set the span to the maxlength of the textbox and then onkeyup it is updated.

It’s working very well, however when the textbox is inside a ASP.NET ModalPopup from the ajax control library, I have observed that if the ModalPopup closes and then is reopened, the $(document).ready doesn’t appear to re-fire so the initial number of characters remaining isn’t updated (although when you start typing again, it works as expected)

(In my example there is a DropDownList inside the ModalPopup with AutoPostBack=”true”, and after postback the .Show() method is called on the popup extender to make it remain visible)

When I’ve come across similar things I’ve been directed to use the .live event in jQuery which I have done for the keyup handler. Is there some similar way to solve the problem for the initial setting of the span?

ASP.NET markup:

<asp:Content ID="Content2" ContentPlaceHolderID="cMain" Runat="Server">
    <p> 
        Enter some text (<span id="countRemain"></span> characters remaining) <br />
        <asp:TextBox runat="server" id="txtCounterTest" MaxLength="100" CssClass="input mediumField CountMe" />
    </p>
</asp:Content>

and associated jQuery:

$(document).ready(function () {
    $(".CountMe").each(function () {
        var txtBoxContent = ($(this).val());
        var txtBoxMaxLength = ($(this).attr("maxlength"));

        var length = (txtBoxMaxLength - txtBoxContent.length) + '/' + txtBoxMaxLength;

        //alert(length);

        $(this).parent().find("#countRemain").text(length);
    });

    $(".CountMe").live("keyup", function (event) {
        var txtBoxContent = ($(this).val());
        var txtBoxMaxLength = ($(this).attr("maxlength"));

        var length = (txtBoxMaxLength - txtBoxContent.length) + '/' + txtBoxMaxLength;

        //alert(length);

        $(this).parent().find("#countRemain").text(length);
    });
});
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T17:38:59+00:00Added an answer on May 28, 2026 at 5:38 pm

    I’m afraid the “ready” event won’t fire again after the ajax has executed.

    Adding “shown”/”hiding” events to the ModalPopupExtender

    I’ve never user the ajax control toolkit so i’m not very familiar with it. Although I have found this add_shown & add_hiding ModalPopupExtender Events article that explains how to attach an event handlers when the popup is shown or hidden.

    This is I guess what you want to achieve: when the modal is shown, re-calculate “how many characters the user has entered and how many they have left”.

    Sys.Application.add_load(pageInit);   
    
    function pageInit()  
    {  
        var modalPopup = $find('ModalPopupExtender1');   
        modalPopup.add_shown(onModalShown);  
    }  
    
    function onModalShown(sender, args)  
    {  
        // this will be executed when the modal is shown.
        // re-calculate here the character thing
    }   
    

    I haven’t tested it to be honest but it should maybe give you a lead.

    Further reading:

    • ASP.net Ajax Client Life-Cycle Events
    • PageRequestManager class

    Event delegation

    Concerning the delegation of the “keyup” event using jquery, you should use .delegate() (jQuery 1.4.3+) or .on() (jQuery 1.7+) instead of .live() as the latter is obsolete now and might be removed from the jquery library in any future version.

    The delegation mechnism is completely different and more efficient and the syntax differs a little bit also:

    $("#someParentContainer").delegate('.CountMe', 'keyup', function(e) {
    
    });
    
    $("#someParentContainer").on('keyup', '.CountMe', function(e) {
    
    });
    

    In words, you will delegate the event “keyup” triggered from an element with class “.CountMe” to a parent element (like you “#Content” panel for instance).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I have a text area in my form which accepts all possible characters from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.