简体   繁体   中英

jQuery Autosize plugin on dynamically added textarea elements

Hi :) I'm using jQuery Autosize plugin to resize some of my textarea elements dynamically. Everything is good but the problem is that when I add some textarea elements to the DOM dynamically, the autosize thing no longer works.

I initialize the plugin like so:

$(document).ready(function () {
        $('textarea').autosize();
    });

I tried to enable the plugin for my dynamically added textareas like:

myDynamicallyAddedTextarea.autosize();

Unfortunately, nothing happened. Can anybody help me with this?

sorry I can't comment yet, where are you adding this textarea at? can you post some of the code around the dynamic generation so that I can see when this stuff is getting called?

according to the docs , all you have to do is something like this for dynamically added elements...

function addTextArea() {
  $(body).append($('<textarea class="test" />'));
  $('.test').autosize();
});

//somewhere in code, but must be after the autosize plugin js has loaded
addTextArea();

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