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

  • Home
  • SEARCH
  • 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 7033457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:04:05+00:00 2026-05-28T01:04:05+00:00

Found this very simple code to show character counts for my text inputs: http://www.jamesfairhurst.co.uk/posts/view/jquery_word_character_counter

  • 0

Found this very simple code to show character counts for my text inputs:

http://www.jamesfairhurst.co.uk/posts/view/jquery_word_character_counter

I modified it like below. However it isn’t working correctly when backspacing. it doesn’t display the char count correctly. Can somebody help fix this?

$(document).ready(function() {
    $('.word_count').each(function() {
        $(this).parent().find('.counter').html('Only ' + $(this).attr("size") + ' characters allowed');
        // bind on key up event  
        $(this).keydown(function(event) {            
            k = event.keyCode;
            // eat backspaces, tabs, and CRs
            if(($(this).attr("size") - $(this).val().length) == 0&&(k!=8&&k!=9&&k!=13)) {
                event.preventDefault();
            } else {
              if($(this).val().length==0) {
                $(this).parent().find('.counter').html('Only ' + $(this).attr("size") + ' characters allowed');
              } else {
                $(this).parent().find('.counter').html(($(this).attr("size") - $(this).val().length-1) + ' characters left');
              }
            }
        });
    });
});

http://jsfiddle.net/Mrbaseball34/RymcJ/16/

  • 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-28T01:04:05+00:00Added an answer on May 28, 2026 at 1:04 am

    There are a lot of things that could be improved about that script. The source of your problem is described by the comment:

    // eat backspaces, tabs, and CRs
    

    Next, it uses the keydown event, so it might have certain other problems. It also takes the count from the size attribute rather than the more correct maxlength attribute. Here’s how I would rewrite it:

    $(document).ready(function() {
        $('.word_count').each(function() {
            var $this = $(this);
            var counter = $this.parent().find('.counter');
            var maxlength = $this.attr('maxlength');
    
            counter.text('Only ' + maxlength + ' characters allowed');
    
            $this.bind('input keyup keydown', function() {
                var value = $this.val();
    
                if(value.length > 0) {
                    counter.text((maxlength - $this.val().length) + ' characters left');
                } else {
                    counter.text('Only ' + maxlength + ' characters allowed');
                }
            });
        });
    });
    

    Here’s a demo.

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

Sidebar

Related Questions

I found this web site (photoblog) http://www.OneReaction.net/ and I am very curious how this
I've found it very weird that simple code like this is not valid: select
EDIT 9-3-10: I found this blog entry recently that was very enlightening. http://optimizermagic.blogspot.com/2007/12/outerjoins-in-oracle.html There
This seems like a very simple question, but I've only found complicated answers. I've
I use ikvm with mpxj project. I have a very simple code like this....
I found this question that was very useful in learning the basics of the
I have always found this to be a very useful feature in Visual Studio.
Very much related to my previous question , but I found this to be
This is a very famous questions in Google. I found several suggestion to achieve
I've found it very difficult to find any existing documentation on this. What I'm

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.