Giter Club home page Giter Club logo

autogrow's Introduction

autogrow.js

endorse

autogrow.js is a jQuery plugin that, like most plug-ins, was built out of frustration for lack of support for a much-needed feature, namely the ability for a textarea to automatically grow and shrink dynamically with its content. I received a head-start from this post, but decided I needed more functionality. It handles the paste event and 'ctrl+x', or cutting.

Basic usage (Interactive Demo):

$('textarea').autogrow(); //or some selector that will grab textareas

autogrow.js has some options that you can set:

  • context: defaults to $(document). The parent element events will be delegated to. If you want to simply use defaults but want to pass in a context, you can just do that like this: $('textarea').autogrow($('.myContext'));
  • animate: defaults to true. Set to false if you don't want the resizing of the box to be animated.
  • speed: defaults to 200. Speed of the resize animation.
  • fixMinHeight: defaults to true. Set to false if you don't want the box to stop shrinking when it hits its initial size.
  • cloneClass: defaults to 'autogrowclone'. Helper CSS class for the clone used for measuring sizes. Use this if you need to apply special rules for a textbox that is right next to the one you're autogrowing, but not exactly it so that they are identical.
  • onInitialize: defaults to false. Will trigger autogrow on init.

Example:

var opts = {
    context: $('li')
    , animate: false
    , cloneClass: 'faketextarea'
};
$('.mytextareas').autogrow(opts);

Events

autogrow:grow and autogrow:shrink events will fire when a grow or shrink animation completes (or immediately if there are no animations). You can get the textarea this applies to by looking at the event.target

The event will be raised against the context you pass into the options.

Example:

$('#context-element').on('autogrow:grow', function(e) {
  var box = $(e.target);
  console.log(box.height());
});

autogrow's People

Contributors

crisfole avatar deefour avatar johnbhall avatar robertmain avatar ultimatedelman avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

autogrow's Issues

Create a destroy method.

You've done a great job however i think its really really important to always have a destroy method available in case you need to bind and unbind such events. If you are willing to add such a feature, i am willing to submit a PR for this.

Thanks, let me know.

jQuery 3 compatibility - error in the resize function

We recently upgraded to jQuery 3.1.0
Now we are getting the following error when we backspace in a textbox, which is strange because we are using autogrow on textareas only.

Unable to get property 'ownerDocument' of undefined or null reference

The error happens on
clone = box.clone() //add clone class for extra css rules .addClass(opts.cloneClass) //make "invisible", remove height restriction potentially imposed by existing CSS .css({ position: 'absolute', zIndex: -10, height: '' }) //populate with content for consistent measuring .val(box.val());

Getting an error

I paste the js file in my project.
I use the $('textarea').autogrow(), but get the following error:
Uncaught TypeError: undefined is not a functionmessage-interaction.js?compile=false:21 (anonymous function) - on the autogrow function

Stops growing when approaching parent height.

I have the textarea inside of a div with a fixed height and overflow: scroll;. When the textarea approaches the height of the parent div (about one line height above). I can't find anywhere that seems like it will cause this problem, but wondering if you have any ideas.

I am looking for it to expand so that the div it is inside of needs to scroll to see the bottom of this.

I will try to recreate the issue in a fiddle.

Textarea not resizing after form submitted

I have an Rails app with a remote form like this

<%= form_for Message.new, remote: true do |f| %>
   <%= hidden_field_tag "room_uid", @room.uid %>
   <%= f.text_area :content, class: "chat-text-field", autocomplete: "off", autofocus: true %>
 <% end %>

when I submit this form, after typed a long text, the textarea does not back to its initial size. I'm calling autogrow with $('.chat-text-field').autogrow(); and submiting the form with

$( ".chat-text-field" ).keypress(function(data) {
      if(data.charCode == 13 && !data.shiftKey){
        $('#new_message').submit()
      }
    });

Function after resize

There should be an option that will take a function name as a paramenter and execute it whenever the textarea is resized.

onInitialize option not working

I have a textarea that contains a lot of text when the page initially loads. I call ...

$('textarea').autogrow({onInitialize: true})

... and it does not grow to contain the text immediately. When I click on the textarea and type something, only then does it autogrow.

Problem with selector?

I'm trying to use autogrow with a lot of other plugins together, but when I try to use a custom jquery selector, it doesn't work.
I created a html attribute on the textarea to control who already has autogrow and who's doesn't.

console.log($('.tarea_marcacao')); // return all textareas
console.log($('.tarea_marcacao[appliedAutogrow="false"]')); // return the same all textareas

$('.tarea_marcacao').autogrow({ //works
    onInitialize: true,
    fixMinHeight:true
});

$('.tarea_marcacao[appliedAutogrow="false"]').autogrow({ //nothing happens...
    onInitialize: true,
    fixMinHeight:true
});

I also tryed to use it inside a $.each loop, but it didn't worked too

$('.tarea_marcacao[appliedAutogrow="false"]').each(function(){
    $(this).autogrow({ //doesn't work either....
        onInitialize: true,
        fixMinHeight:true
    });
});

Thanks.

Wrong height with padding

Hi,

Seems adding a padding to textarea cause resizing having wrong height value.
You'll find a test case here : http://jsfiddle.net/Akaryatrh/wfUcM/

Moreover, focus on textarea seems required to activate autogrow. Is there a way to calculate and assign height to elements when plug-in is called ?

textarea shrinks below initial size

If I delete all the text in a textarea, the box shrinks below it's initial size. I tried setting fixMinHeight to false as well and I am not sure what else to try. Is this supposed to happen?

Minimum height and select all + delete and onClick

Thanks for an awsome plugin! I have three minor issues though:

  1. fixMinHeight doesn't seem to do anything for me. I've tried with setting the row attribute and height in my markup, but those values are ignored when backspacing all to the top. The box size follows and shrinks all the way until one line remains.
  2. Select all + delete. The box remains extended.
  3. The textarea doesn't expand when clicking in it, you have to start typing. This is perhaps not an issue, but it would be nice to have it in options? (I solved it by adding .on('click', selector, resize) at line 30.)

Set maxheight

Is there a way to set maxheight so it can stop growing up to some point?

Sets focus to textarea

When I run autogrow it seems to change the focus to the textarea, causing my page to immediately scroll down.

How can I prevent that?

Initially hidden textareas

First of all, thanks for the great plugin, I really love it!

Issue: if the autogrown textarea is initially hidden (say under a different Bootstrap tab that's not currently visible) its height is still adjusted and since it's not visible, empty textareas end up shrinking to nothing.

image

I believe this issue can be solved by simply not autogrowing invisible empty textareas...

Does not work with jQuery 3.x

Easy to reproduce: Use jQuery 2.x and it works fine. Switch to new jQuery 3.x release and it does not work anymore

Not autogrowing textarea not visble

Hi, i have a textarea into of a modal bootstrap, via js initialize this textarea with autogrow but this not auto resize, i think this happens because this texarea is visible only modal launch. Any solution to this.

Thanks.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.