简体   繁体   中英

Add character counter in simple_form in Ruby on Rails

I use the simple_form gem and I want a simple character counter on a text field. I was told, this might work:

add this to the 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 this information from here (Attention: It is 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, but nothing happens. Here is my actual code chapters/new.html.erb:

    <%= simple_form_for([@book, @book.chapters.build]) do |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 you give me any advice, how to get it work?

You need to wrap your code in jquery document ready function:

$(function() {
  $("#body-field").on("keyup", function(){
    var length = $(this).val().length;

    $("#body-count").html(length);
  });
});

Why don't you use an existing library instead? https://github.com/dtisgodsson/jquery-character-counter

You might want to use either js or coffee-script, I am providing a coffee script example below:

Add this piece of code to your chapters.coffee file:

ready = ->
  totalChars = 100
  #Total characters allowed
  countTextBox = $('#body-field')
  # Remaining chars count will be displayed here
  charsCountEl = $('#countchars')
  #initial value of countchars element
  charsCountEl.text totalChars
  #user releases a key on the keyboard
  countTextBox.keyup ->
    #get chars count in Text field
    thisChars = @value.replace(/{.*}/g, '').length
    if thisChars > totalChars
      # total extra chars to delete
      CharsToDel = thisChars - totalChars
      #remove excess chars from text field
      @value = @value.substring(0, @value.length - CharsToDel)
    else
      #count remaining chars
      charsCountEl.text totalChars - thisChars
    return
  return

$(document).ready ready
$(document).on 'page:load', ready
# Loads javascript while loading page

Add this line to your form right below to the Text input field.

 var ready; var charsCountEl, countTextBox, totalChars; totalChars = 100; countTextBox = $('#body-field'); charsCountEl = $('#countchars'); charsCountEl.text(totalChars); countTextBox.keyup(function() { var CharsToDel, thisChars; thisChars = this.value.replace(/{.*}/g, '').length; if (thisChars > totalChars) { CharsToDel = thisChars - totalChars; this.value = this.value.substring(0, this.value.length - CharsToDel); } else { charsCountEl.text(totalChars - thisChars); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <input type="text" id="body-field" /> <span id="countchars"></span> 

If your file under javascript/ folder doesn't have extestion .coffee the please rename it to chapters.coffee if it does then thats it.

PS: here is the javascript version of the same http://pastebin.com/LZb1DAC4 .

Reference : https://stackoverflow.com/a/24629105/2545197

This solution won't be as good as Abhinay's solution, but it works for me: (Please note, I'm an amateur, this code may be horrendous)

Javascript code:

<script>

  $(document).ready(function(){

    var choosingbar = function( event ){
      $(event.target).parents(".counting").children(".count").text($(event.target).val().length);
    };

    $(".counting textarea").keyup(choosingbar);
  });

</script>

new.html.erb code:

<%= simple_form_for([@book, @book.chapters.build]) do |f| %>
    <%= f.input :chaptertitle %>
    Mininmum amount of characters: <%= @book.min_length %> Maximum amount of characters: <%= @book.max_length %>
    <div class="counting" ><%= f.input :chaptercontent %>Characters: <span class="count"></span></div><br>
    <%= f.input :author %>
    <%= f.button :submit %>
<% end %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM