simple form - Add character counter in simple_form in Ruby on Rails -
i use simple_form gem , want simple character counter on text field. told, might work:
add form:
<%= f.input :body, id: "body-field" %> <span id="body-count">0 characters</span>
and javascript:
$("#body-field").on("keyup", function(){ length = $(this).val().length; $("#body-count").html(length); });
i got information here (attention: full of advertisement): http://www.sohua.xyz/questions-full/4320915/how-do-i-implement-a-basic-character-counter-in-a-simple-form
i did this, nothing happens. here actual code chapters/new.html.erb:
<%= simple_form_for([@book, @book.chapters.build]) |f| %> <%= f.input :chaptertitle %> mininmum amount of characters: <%= @book.min_length %> maximum amount of characters: <%= @book.max_length %> <%= f.input :chaptercontent, id: "body-field" %> <span id="body-count">0 characters</span> <%= f.input :author %> <%= f.button :submit %> <% end %> <script> $("#body-field").on("keyup", function(){ length = $(this).val().length; $("#body-count").html(length); }); </script>
can give me advice, how work?
you need wrap code in jquery document ready function:
$(function() { $("#body-field").on("keyup", function(){ var length = $(this).val().length; $("#body-count").html(length); }); });
why don't use existing library instead? https://github.com/dtisgodsson/jquery-character-counter
Comments
Post a Comment