jackmoore/autosize

Autosize does not work on AJAX loaded textarea

Closed this issue · 2 comments

$.ajax({
type: "POST",
url: "load_textarea.php",
.....
$('#wall-posts').html(result);
});

Anyone experienced the same issue?

Autosize only works with existing elements (not selectors that may match new elements at a future time). So you would want to assign Autosize after your targeted textarea element is in the DOM and is visible. For example:

$.ajax({
type: "POST",
url: "load_textarea.php",
.....
$('#wall-posts').html(result);
autosize($('textarea'));
});

However, I assume you could setup a MutationObserver to automatically assign autosize to new textarea elements.

A lighter touch solution would be to use event delegation to assign Autosize on focus. For example:

document.addEventListener('focus', function(e){
	if (e.target.nodeName === 'TEXTAREA') {
		autosize(e.target);
	}
}, true);