最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Autosize jQuery plugin, trigger a autosize.update - Stack Overflow

programmeradmin5浏览0评论

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: - 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!

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!

Share Improve this question edited Mar 19, 2015 at 23:27 Rooster 10.1k8 gold badges48 silver badges72 bronze badges asked Mar 19, 2015 at 23:19 ArvyArvy 1,2122 gold badges18 silver badges32 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 12

you can use it as follows:

autosize.update($('textarea'));

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');

}

In my case I had to update the size of textarea not only based on changed content (this is handled by "autosize" plugin itself nicely) but based on CSS properties change as well, e.g. when user is using a slider to change the font-size of the textarea.

This worked for me:

// ... slider ui init bla bla
slide: function(){
  var $textarea = $('.your.selector.for.textarea.or.cached.jQuery.object.with.target.textarea');

  // .. changing the actual css property 

  // triggering the .autosize
  $textarea.trigger('autosize.resizeIncludeStyle');

  // ... etc etc
}
// ...

This is not properly described in the Autosize Documentation but the event does exist and is handled in the jQuery version of the plugin.

Autosize creates a custom event, that can be triggered manually like this:

$("thextarea").trigger("autosize");
发布评论

评论列表(0)

  1. 暂无评论