Going through the Railstutorial exercise (chapter 10), I used Jquery to count the remaining characters in Textarea. It really works , but only when I refresh my page at least once for each click . this means that the request is not executed until the page is refreshed one time after each click and where it will work perfectly. I used css for the .countdown method.
So my question is: why do I need to refresh the page to see the remaining characters on the page, and also are there any better methods. Can someone tell me what is happening here?
Css code
.countdown {
display: inline;
padding-left: 10px;
color: #338333;
}
Here is the code for micropost.js.coffee
updateCountdown = ->
remaining = 140 - jQuery("#micropost_content").val().length
jQuery(".countdown").text remaining + " characters remaining"
jQuery ->
updateCountdown()
$("#micropost_content").change updateCountdown
$("#micropost_content").keyup updateCountdown
here is the contents of _micropost_form.html.erb
<%= form_for(@micropost) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field">
<%= f.text_area :content, placeholder: "Maximum characters: 140" %>
</div>
<%= f.submit "Post", class: "btn btn-large btn-primary" %>
<span class="countdown"></span>
<% end %>
Here is the image when I log in and go to the home page ( without refreshing the page )

And here is the image, when I login, go to the home page and refresh the page

source
share