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 8401221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:45:00+00:00 2026-06-09T21:45:00+00:00

I am trying to use a code snippet I found online to implement charcount.

  • 0

I am trying to use a code snippet I found online to implement charcount. It works for one single text area. I have multiple text areas with different count limits. Here is the code that works for one single form.

Javascript:
function countChar(val,count,focus){

        var len = val.value.length;
    var lim=count;
    var focussed=focus;

    if (len >= lim) {

            $('#charNum').text(lim - len);
            $('#charNum').addClass('exceeded');
        /* val.value = val.value.substring(0, lim);*/   
        }else {
        if(focussed===0){
            $('#charNum').html('<a>&nbsp;</a>');
        }
                else {
            $('#charNum').text(lim - len);
            $('#charNum').removeClass('exceeded');
        }
        }
};

HTML:
<div id='charNum' class='counter'>&nbsp;</div>
<textarea id='description' name='description'  onkeyup=\"countChar(this,200,1)\" onblur=\"countChar(this,200,0)\" rows='10' cols='20'></textarea>

If I have two text areas, how can I modify this code to work ? I know how to get the id of the div in the script, But I dont know how to correctly update the counter div, say #charNum1, and #charNum2. Appreciate some hints. Thanks

EDIT:
I was thinking, I can name the counter div as “Charnum+divName” if that helps

  • 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-06-09T21:45:02+00:00Added an answer on June 9, 2026 at 9:45 pm

    If you attach your event handler(s) with jQuery you can use this within the handler to refer to whichever element the event was triggered on, thus avoiding having to hardcode element ids inside your function.

    I’d suggest adding an attribute with the max chars allowed and removing the inline event handlers:

    <div id='charNum' class='counter'>&nbsp;</div>
    <textarea id='description' name='description' data-maxChars="200" rows='10' cols='20'></textarea>
    <div id='charNum2' class='counter'>&nbsp;</div>
    <textarea id='otherfield' name='otherfield' data-maxChars="400" rows='10' cols='20'></textarea>
    

    Then:

    $(document).ready(function() {
        $("textarea[data-maxChars]").on("keyup blur", function(e) {
            var $this = $(this),
                $counter = $this.prev(),
                len = $this.val().length,
                maxChars = +$this.attr("data-maxChars");
    
            $counter.text(maxChars - len).toggleClass("exceeded", len > maxChars);
        });
    });    
    

    Demo: http://jsfiddle.net/nnnnnn/GTyW3/

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

Sidebar

Related Questions

i am trying to use this code: <%= File.ReadAllText(Server.MapPath(Members/newsletters/welcome.html))%> which works great but now
Below is a snippet of code i am trying to use to search an
Trying to use this code to connect the AD PrincipalContext context = new PrincipalContext(ContextType.Domain,
I'm trying to use the code review feature when I check-in my files but
I am trying to use this code in my QT app QMap<QString,QMap> but there
I am trying to use the code from Microsoft for an Async Socket connection.
I'm trying to use some code like this, and it's failing: data = Range(A1)
I am trying to use http://code.google.com/p/as3svgrendererlib/ in my flash cs 5.5 project to import
I was trying to use this code: if($_SERVER['HTTP_PORT'] !== 443 && !isset($_SERVER['HTTPS'])) { header('Location:
I am trying to use the following code, to display $formcode, which is the

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.