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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:37:33+00:00 2026-05-23T06:37:33+00:00

I found a jquery snippet of code for counting down the characters when you

  • 0

I found a jquery snippet of code for counting down the characters when you type in a form. I’ve tried adding it to my application.js file but its not working. When I type in the textarea the number 350 doesn’t change.

I’ve already installed jquery-rails and I use <%= javascript_include_tag :all %> so that shouldn’t be the issue. Here’s what I have in my application.js

$('textarea').keyup(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);

In my view I have :

<%= form_for @post, :validate => true, :html => {:multipart => true} do |f| %>
  <%= render 'shared/error_messages', :object => f.object %>
    <div class="field">
      <%= f.label :title, 'Title:' %><br /> 
    <%= f.text_field :title %><br />
    <%= f.label :image, "Choose image"%><br />
    <%= image_tag(@post.image_url(:avatartiny).to_s) if @post.image? %>
    <%= f.file_field :image %>
    <%= f.hidden_field :image_cache %><br />
    <%= f.label :content %><br /> 
    <%= f.text_area :content %><br />
    <div id="counter"><span>350</span> characters remaining.</div>
   </div>
  <div class="actions">
    <%= f.submit "Submit" %>
  </div>
<% end %>

And in my custom.css I have:

textarea {
   width:400px;
   height:400px;
   border:1px solid #999;
   border-radius:3px;
   padding:7px;
}

#counter span { font-weight:700 }

Here is what my html source says:

    <form accept-charset="UTF-8" action="/posts" class="new_post" data-validate="true" 
enctype="multipart/form-data" id="new_post" method="post" novalidate="novalidate"><div 
style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" /><input
 name="authenticity_token" type="hidden" value="V6J0VERvuoKvQ5kXXgBuU5aC6VsKQ7W6RF30IKy9D7E="/></div>

<div class="field">
      <label for="post_title">Title:</label><br /> 
<input data-validate="true" id="post_title" name="post[title]" size="30" type="text" /><br />
<label for="post_image">Choose image</label><br />

<input data-validate="true" id="post_image" name="post[image]" type="file" />
<input id="post_image_cache" name="post[image_cache]" type="hidden" /><br />
<label for="post_content">Content</label><br /> 
<textarea cols="40" id="post_content" name="post[content]" rows="20"></textarea><br />
<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-23T06:37:33+00:00Added an answer on May 23, 2026 at 6:37 am

    Wrap your textarea binding in a document.ready binding:

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

    Does that work?

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

Sidebar

Related Questions

I am developing a mobile web app. Currently I have this snippet of jQuery
does extjs contain method equivalent to inArray? i checked the API but found nothing!
Are the end results of the following jQuery snippets identical? Snippet 1: $(function() {
Possible Duplicate: Learning jQuery and Getting Better at Javascript I have found jQuery very
Begin with ajax and jquery combined with rails, I am trying to create a
I am sorry if this has been asked already, but I have been looking
I'm quite new on jquery and i was looking for something that could replace
I've been using jQuery UI autocomplete and it works perfectly in the development side.
I have been trying to use Jeditable to make my html table editable. However
I've had to implement some changes to a user control that's used in a

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.