Git Product home page Git Product logo

autosize's Introduction

Summary

Autosize is a small, stand-alone script to automatically adjust textarea height to fit text.

Demo

Full documentation and a demo can be found at jacklmoore.com/autosize

Install via NPM

npm install autosize

Usage

The autosize function accepts a single textarea element, or an array or array-like object (such as a NodeList or jQuery collection) of textarea elements.

// from a NodeList
autosize(document.querySelectorAll('textarea'));

// from a single Node
autosize(document.querySelector('textarea'));

// from a jQuery collection
autosize($('textarea'));

Released under the MIT License

autosize's People

Contributors

antriver avatar builtbylane avatar firedev avatar genadis avatar grrowl avatar hefloricardo avatar hnguyen08 avatar jackmoore avatar jam13 avatar jclement avatar joaocunha avatar kanecohen avatar korny avatar ledermann avatar leonidaz avatar markusgut avatar nazar-pc avatar neilb avatar nickbaum avatar ninir avatar njam avatar onionhammer avatar revisor avatar rmcauley avatar sabl0r avatar seguemark avatar stof avatar ttsuru avatar vezul 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  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

autosize's Issues

Tabindex problem

When the user presses the tab key, it will also go through the hidden textareas you create (the ones with the "autosizejs" class). The fix is simple: add a tabindex="-1" property to the textarea tag.

Textarea with one row bug in Firefox.

I have a textarea with one row. In chrome it just works fine, the textarea initially has one row. But in firefox the textarea starts with 2 rows by default. I have checked my css if some property is causing that but didn't find nothing related.

I'm using Firefox 15.0.1

Thanks

on window resize

$(window).on('resize', function(){
gives an error under js console,here

$(window).resize(function(){
works for me (evein if it is set to false)

this to get adjust() under responsive layouts

(I wonder why I can't trigger it with 'focus blur etc..' events)

Strange behavior with resize: vertical in chrome 20

Hi!

I'm in doubt if I should post this issue here, looks like it is Chrome problem.
Anyway.

I'm applying plugin, then forcing it back to certain height and make resizable vertically with code:
$('textarea').autosize().css({'resize': 'vertical', 'height': '96px'});

And here comes strange things. If i resize textarea, then edit text I'll be able to resize it with mouse back to 96px.

But if I edit text so textarea grows, say to 200px and only after that I'll try to resize it , minimal height will be limited to 200+ px.

The same code in FF doesn't make any constraints, i can resize down till one row height.

wrap="off" + autosize prevents editing long lines

See this js fiddle: http://jsfiddle.net/Dsb87/1/

Autosize doesn’t recognize textareas with wrap=off. Even if enabling the horizontal scrollbar explicitly in CSS it is disabled by Autosize. Instead, it adds an additional line so that the text would fit if wrapping were on.

The use case for such a textarea is where the line breaks matter (i.e. one option per line).

Max-height option

Why don't you add a max-height (in rows or px) option. It is not always useful to have a huge textarea, but limit it's height with scrolling

Odd Height Miscalculation on Hidden Textarea

So my app consists of some comments that can be edited inline. The process I go through goes something like this. When a comment is created, I apply autosize() on to the hidden textarea used for editing. My problem is, is that when I show() the hidden textarea, the height of the textarea is miscalculated, and the result is a textarea that is far too big. Allow me to explain with images:

  1. Page renders with hidden textarea, and autosize() is applied to it.

Step 1

  1. User clicks on edit to show textarea.

Step 2

  1. User does a 'keyup'.

Step 3

As you can see, right when a the user clicks the edit link, the height of the textarea is wayyyy to big. I'm curious as to why this is happening. I'm on the latest version of Chrome.

Min-height option

Why don't you add a min-height option.
Using min heigh option allows you to specify a minimum height that the object will have even if the field is empty.

Unfortunately, setting the min-height in my CSS, autosize does not respect it.

Resizing is buggy with border/padding

I am using it with Twitter Bootstrap's span12 class and the resize sometimes happens well after I am a couple of characters into the next line. It should happen as soon as the word wraps on the next line.

If I set the textarea to padding:0; border:0 it works. Alternatively, if I set the width in absolute px it works as well.

UPDATE 1: Turns out even setting a fix width doesn't fix the issue. Only padding:0 and border:0 fixes it. The rounding error mentioned prior is not correct since I am on Chrome.

Sizing textareas with existing content.

I experience the exact same behavior as described in the ticket 'Odd Height Miscalculation on Hidden Textarea'. Having multiple text areas on the page, those after the first one grow to ridiculous sizes and this goes for all browsers.

Now I read over the version log, and noticed you reworked it so only one mirror is used.

I then tried version 1.14, and everything works as it should, so sadly the update you did in 1.15 broke it.

plugin is adding additional height to textareas (webkit)

just to mention...

starting with textarea containing 1 line of text, and the following styles:

height: 17px;
line-height: 17px;
font-size: 14px;
box-sizing: content-box;

the plugin sets textarea height to 25px..

seems to affect webkit only (tested latest Chrome, Safari), Firefox is fine textarea height is as expected.

rows does not reset if text is removed via .val('')

First of all, this plugin awesome - thanks for creating it! :)

So when I enter text which grows the number of rows, and then remove the text via .val(''), the height does not reset. Is there a simple workaround to this?

iOS scrollTop bug with <audio>

The Problem

It's hard to explain, so we did a screencast showing the problem:
http://youtu.be/9Zws0zyI9jc
The page jumps/scrolls up and down as you input text into the textarea. In other cases weren't able to input text anymore after some characters

Reproduce

  • This happens only in iOS
  • There is more than 1 textarea
  • Only if there is an <audio> element

You can reproduce this with the iOS Simulator using the following jsFiddle:
http://fiddle.jshell.net/YkTUF/0/show/

The editable version: http://jsfiddle.net/YkTUF/

box-sizing: border-box causes the height to be bigger than what it should

Hey,
I'm using your plugin within Twitter Bootstrap, applying it in a textarea.input-block-level, which has box-sizing: border-box.

This causes the one-line height of the textarea to be bigger than what it should.
I commented the line 70 of the plugin code, than I get the correct initial height of my textarea.

See the image below:
textareas

Ajax pages

Hi there,
I have looked for a way to make this plugin work with ajax pages. This works fine in all browsers I need to support except ie8. There the update that is triggered by on(resize) fails and gives a error.

Allso on larger applications the plugin leaves a lot of bindings on de window so I think e destroy function would be the best solution.

Please provide one.

Wrong height when using box-sizing: border-box

I tried using your plugin in an application using Twitter Bootrap, where textareas have box-sizing: border-box by default, and noticed that the textarea was not resizing properly (it lagged behind a bit).

After fiddling around a bit, I saw this line in the adjust() function:

mirror.style.width = $ta.css('width');

Doesn't this need to be changed in order to take horizontal padding into account when using border-box? Like this:

mirror.style.width = Math.max(0, $ta.width()) + "px"; // IE8 does not like negative widths

This seems to solve my issue. Haven't tested it thoroughly with other box-sizing values though. Any thoughts on this?

line-height missing?

If I set the textarea's line-height to a larger value (e.g. 2em) than default. autosize seems not to consider it and still use the default line-height value. This cause the textarea's height is always smaller than the height of the text inside (if the line-weight is larger than the default), and some text can not show.

Adding "lineHeight" to copyStyle seems to solve this problem in my Firefox. Not sure about other browsers.

IE8: animating textarea height does not work

Here is an example: http://jsfiddle.net/8bmpd/
This is expected to be a textarea with autosize with a Collapse/Expand button.

$('#ta').autosize();
$('#toggle').click(function (e) {
e.preventDefault();
var $ta = $('#ta');
if ($ta.hasClass('collapsed')) {
$ta.removeClass('collapsed');
$ta.stop().animate({height: $ta[0].scrollHeight}, 'fast');
} else {
$ta.addClass('collapsed');
$ta.stop().animate({height: 48}, 'fast');
}

It is working as expected in ff, chrome and IE9.
But in IE8 nothing happens when you click the "toggle" button. However if we set the height with .css() - it is working: http://jsfiddle.net/8bmpd/1/

Do you have any suggestions or workarounds on this matter?

IE8 Issue

Hello,

I have discovered an issue with IE8. But only in pure IE8 and not IE10 using IE8 mode.

Giving the following example


<!DOCTYPE html> 
<html>
    <head>
        <title>Textarea Autosize Demo</title>
        <script src='http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js'></script>
        <script src='jquery.autosize.js'></script>
        <script>
            $(function(){
                 $('.formTextArea').autosize();
            });
        </script>
    </head>
    <body>
        <textarea class="formTextArea"></textarea>
        <br/>
        <textarea class="formTextArea"></textarea>
    </body>
</html>


It doesn't resize, but if I refresh the page it starts to work. However, as soon I remove it works fine.

Thanks
Cheryl

Cannot input any text on iOS 4

I didn't investigate this further, but the plugin doesn't seem to work on iOS 4.3.3 and iOS 4.2.1. You can click on the text area, the keyboard comes up, but then the cursor vanishes and you can't enter any text.

It works fine with iOS 5+.

RTL direction problem

Hi,

I really like your autosize script and I am using it in almost all projects. Recently I "tripped" over an issue and I can't solve it. When I do page in RTL, for example for Arabic the css direction:rtl conflicts autosize. Page gets very wide with horizontal scroll...

Here is example:
http://cdpn.io/ltyfa

If anyone could help, I would be very gratefull. Thanks!

how to disable autosize on an element?

Sorry if this has been handled somewhere...

I want to experiment with using autosize when a textarea has focus, but then to return it to its previous height upon blur.

But I don't see a way to turn off autosize once you enable it for an element... is there a way?

mirror elements not removed from DOM

Hi again! Sorry to bother you again but I found that the mirrored textareas aren't being removed from the DOM once the original textareas are gone. I fixed this by binding each textarea to jQuery's remove event and then removing the mirror in there. I thought I would let you know :)

In FF the box doesn't resize correctly

When in FireFox, the height of the box doesn't correctly change when any CSS is applied to it. Incredibly frustrating. It resizes smaller so text gets cut off.

Autosize breaks when used in Chrome extensions

Chrome's content security policy in manifest version 2 doesn't allow autosize.js to run. It fails with the following error in the console:

Refused to execute inline event handler because it violates the following Content Security Policy directive: "script-src 'self'

The error can be traced to the check that tries to weed out old browser versions (specifically the setAttribute line):

// For testing support in old FireFox
test.setAttribute(oninput, "return");

if ($.isFunction(test[oninput]) ...

Is there any other way that this test could be written? Is there a reason that the autosize plugin shouldn't run in Firefox 3-, and can that specific feature be tested for instead?

One line of textarea is hidden in FF 12

Hi, Jack!

I am trying to use autosize plugin, but in FireFox 12 I have situation like this:

ex

As you can see one line is hidden. Can you help me to fix this?

Sincerely,
Anatoli

text cut of because of padding

hi
i would think the plugin takes into account the padding of the textarea...in my case it seems like it's not, because adding padding gets my last line cut off, while removing the padding shows the text without being cut off. i'm on chrome. any ideas? regards

Fix global leak with autosize() function

jquery auto-resize has a global leak.

I wrote a test case to show the leak. Simply download the html file on the following gist and open it in your favorite browser and you will see mocha finding a global leak when using the autosize() function.

Global Leak Testcase

Too many mirrors

If a page contain large number of textareas and I've applied autosize to all of em then there will be too much mirrors appended in the body.

What I suggest is to append the textarea only when textarea got focus. no need of mirror beside that.

here is what I am suggesting

$ta.focusin(function(){
   $('body').append(mirror);
   adjust();
});
$ta.focusout(function(){
  $(mirror).remove();
});

Ghosts div at bottom of page

For each of textareas that are in a page, the plugin is appending divs to the bottom of body.
It should be improved to a cleaner way.

GitHub Logo

how to do callback

hey hi
how do i run the callback?
is it not:
$('.textarea').autosize(function(){ alert('howdy'); });
regards

Plugin seems to override css max-height attribute

This is a nifty plugin but is there a way to set a max height to the textarea? It seems the css max-height attribute gets overwritten in this line:

ta.style.height = ta.style.maxHeight = mirror.scrollTop + 'px';

Is it possible to make it expand up to a certain height then scroll from there?

Thanks

non-default line-height in Opera

Opera has a bug where it uses the browser default line-height for non-visible textarea elements, regardless of user specified line-height. Is there a way to handle this plugin without line-height?

IE9: Height calculated wrong due to font-mismatch

By default, browsers render most form elements (textareas, text boxes, buttons, etc) using OS controls or browser controls. So most of the font properties are taken from the theme the OS is currently using.

IE9 changed this, but not completely...

In the example the font-family defined for the div is used for the mirror, but not for original textarea.(where it is Courier New)

The solution can be to define the style for a textarea:

textarea {
font-family: inherit;
font-size: inherit;
}

Example:
<!DOCTYPE html><html>   
<head>
<meta charset='utf-8'/>     
<meta http-equiv="X-UA-Compatible" content="IE=9" />
<title>Textarea Autosize Demo</title>       
<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js'&gt;&lt;/script>     
<script src='jquery.autosize.js'></script>      
          
<style>
#container { 
    font-family: "Lucida Grande", Tahoma, Arial, Helvetica, sans-serif;
}
</style>
<script>  
$(function(){
$('textarea').autosize();
});     
</script>   
</head> 


<body>
<div id='container'>
<textarea cols='50' rows='5'></textarea>
</div>   
</body>
</html>

Unexpected extra height to textareas in hidden divs (IE7)

I am using the autosize plugin with jQuery tabs on a page. I run the $('.autosize').autosize() command on all textarea elements I want to resize at the time of the page load.

This works fine in IE9 but when I use IE7, the textareas in hidden divs get an unexpected large height and some textareas even expand below the height of the viewport.

Can you check this?

Initial size of a textarea inside an accordion

This is a great autosizer, but I think I've found an issue with it. I have multiple textareas on multiple accordion tabs. When I expand an accordion tab that contains an autosizing textarea, the initial size is huge, but once I start typing in the textarea, it shrinks to the proper size.

I did a bit of debugging and if I put this: alert(height); at line 125 of jquery.autosize.js (version 1.16.4), I do indeed get very large values for the height. Those values drop to proper size when I start typing. So it seems the height = mirror.scrollTop; line is returning an odd value for accordion tabs that are shrunk.

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.