Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autosize jQuery plugin, trigger a autosize.update

I'm using Autosize to automatically resize textboxes (height). The plugin is JS but can be used as a jQuery plugin (explained in the site). The author explains how to trigger a manual "update" event (JS) when you change the text using JavaScript. I need to do the same, but using jQuery "mode", because I'm creating textboxes dynamically using Ajax. Tried trigger() with no success.

autosize(ta); /*Pure JS*/
ta.value = "Something really long";
var evt = document.createEvent('Event');
evt.initEvent('autosize.update', true, false);
ta.dispatchEvent(evt);

Here is a piece of my source code: http://pastebin.com/049UfkGv - after $(this).autosize(); I need to trigger "autosize.update" event to redim the textbox to its new contents.

jQuery Code:

 $(document).ready(function(){
  window.jQuery.fn.autosize=function(){ return autosize(this); };
 });

 $('.edit4').each(function(){
   $(this).keypress(function(event) { if (event.keyCode==13) { event.preventDefault(); };
   $(this).autosize();
 });

Thank you!

like image 425
Arvy Avatar asked Nov 30 '22 10:11

Arvy


2 Answers

you can use it as follows:

autosize.update($('textarea'));
like image 86
user947668 Avatar answered Dec 09 '22 12:12

user947668


In my case the Autosize jQuery plugin could not calculate the right height, because the textarea was hidden on pageload.

The following tigger helped me out:

function showMyTextarea(){

  // do your stuff here

  // trigger autosize     
  $('textarea').trigger('autosize.resize');

}
like image 34
Stefan Vogt Avatar answered Dec 09 '22 11:12

Stefan Vogt