In my Rails 5 app I'm trying to display the amount of characters remaining for the following text area in the view with tag:
<%= form_for @note, :url => registrant_notes_path(@registrant) do |f| %>
<div>
<%= f.text_area :body, maxlength: 1000, style: 'width:100%;', id: "review_text" %>
<%= f.submit "Save", class: "btn btn-primary" %>
</div>
<% end %>
<script>
$(document).ready(function() {
var review_text = $("#review_text");
var counter = $("#counter");
var max_length = counter.data("maximum-length");
counter.text(max_length - review_text.val().length);
review_text.keyup(function() {
counter.text(max_length - $(this).val().length);
});
});
</script>
How to get rid of this script tag and move it to some js file? I created new file under app/assets/javascripts/notes.js with below code:
$(document).on('turbolinks:load', function() {
var review_text = $("#review_text");
var counter = $("#counter");
var max_length = counter.data("maximum-length");
counter.text(max_length - review_text.val().length);
review_text.keyup(function() {
counter.text(max_length - $(this).val().length);
});
});
but then this JS code stops working - it stops counting down. Should I add this notes.js somewhere or am I missed something else?