1

I am working on an app in rails and am a bit new to it. All i want is to create a dynamic textarea that will expand in height based on how much text is written and on update the post to recognize the height of the post accordingly. I have found many examples and even tried autosize gem but for some reason is just not working. what i have so far:

_form.heml.erb partial

<%= form_for @post, remote: true do |form| %>
  <% if post.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(post.errors.count, "error") %> prohibited this post from being saved:</h2>

      <ul>
      <% post.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= form.label :body %>
    <%= form.text_area :body, id: :post_body %>
  </div>

  <div class="actions">
    <%= form.submit %>
  </div>
<% end %>
<div class="output"></div>

posts.js

 $('#post_body').on('change keyup keydown paste cut', 'textarea', function () {
            $(this).height(0).height(this.scrollHeight);
        }).find('textarea').change();

.scss file

textarea {
    width: 200px;
    height:15px;
    line-height:15px;
    min-width: 200px;
    max-width: 300px;
    transition: width 0.25s;
    resize:none;
    overflow:hidden;
}

I have tried many more but for some reason none worked for me and I don't understand why. All files are visible (i have tried some alert('tests')). I use rails 2.5.0 on Linux Ubuntu. If anyone can tell me what I'm doing wrong I'd be very grateful. Thank you.

2 Answers2

1

I'm just use autogrow plugin for jquery: http://www.technoreply.com/autogrow-textarea-plugin-3-0/

In your case:

$(function () {
  $('#post_body').autoGrow();
});

will do the work.

Inpego
  • 2,657
  • 13
  • 14
0

Not sure but you should be able to attach this to the event

$("#your id or . for class").css( "height", "3vw", "width" "10vw");

user1898553
  • 91
  • 1
  • 3