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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:54:05+00:00 2026-05-26T08:54:05+00:00

I am new to JavaScript and I am trying to add a character counter

  • 0

I am new to JavaScript and I am trying to add a character counter to a text field using this code:

$(function(){
  $('.txtclass').keypress(function(){
      ta = $(this);
      if(ta.val().length >= 350){
          ta.val( ta.val().substr(0, 350) );
      } else {
          $("#counter span").text(350-ta.val().length);
      }
  });
});

setInterval('$("#counter span").text(350-ta.val().length);', 350);

I get that JavaScript error: "Error: ta is not defined" and points me to that specific line in my code.

Any insight into whats happening and how to fix it would be really appreciated as I’d like to add more counters for other text-fields into the website elsewhere.

Edit: here’s the html for the counter

 <div id="counter"><span>350</span> characters remaining.</div>
    </div>
  • 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-26T08:54:05+00:00Added an answer on May 26, 2026 at 8:54 am

    It means just what it says. The value being passed to the text method is the result of the expression 350 - ta.va().length. The variable ta has not been defined at the point this code is being run.

    A few things to note before my proposed solution.

    1. You are correct in responding to the keypress event to truncate the user input, but there is no need to use an interval to update the count – just update it in the event handler.
    2. Don’t put the function passed to setInterval in quotes. This causes it to be run through eval, which is both slower and less secure.
    3. Save jQuery objects in variables outside of the event handler so the same objects can be used each time the handler is invoked. This uses less memory and should be a tiny bit faster.

    Here’s how I would implement this:

    $(function () {
      var ta = $(".txtclass"),
          counter = $("#counter span");
    
      ta.keypress(function () {
        var taValue = ta.val();
    
        if (taValue.length >= 350) {
          taValue = ta.val(taValue.slice(0, 350)).val();
        }
    
        counter.text(taValue.length);
      });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to add a text field to a form dynamically using javascript. From
I'm new to jQuery and I was trying add a character counter to a
Hey there, new to AJAX, JS.. Im trying to add to a Javascript/Ajax search
I am trying to create a new Element in my javascript code and append
New to javascript/jquery and having a hard time with using this or $(this) to
I am trying to add a new row in a table via javascript. I
I'm trying to add upload controls to the page. This HTML mark-up with JavaScript
I am new to JQuery. I am trying to add some code in my
New to javascript and jQuery, but I'm trying to dynamically add buttons and then
I'm trying to make a new spine javascript app using d3 (generated using Spine.app).

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.