while going through the exercise on Railstutorial(chapter 10), i have used Jquery to count remaining characters in Textarea. It actually works but only, when i refresh my page for atleast one time per signin. that means query is not executing until the page is refreshed for one time after every signin and there after it is working perfectly. I have used css for .countdown method.
So, my question is..why it is required me to refresh page to see remaining chars on the page and also, is there are some better methods.Can Some one suggest 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 content 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 login and go to Home Page (without refreshing the page)
and Here is the image when i login,go to Home Page and refresh the page