bgrins / ExpandingTextareas

jQuery plugin for elegant expanding textareas
http://bgrins.github.com/ExpandingTextareas/
MIT License
260 stars 73 forks source link

Add val function #23

Closed Crisfole closed 11 years ago

Crisfole commented 11 years ago

Setting the value of an expanding textarea fails to trigger any of the usual re-size callbacks. I added a val function to expanding textarea so that you can set it and have it resize itself after it was set.

Usage:

$('textarea').expandingTextarea();
$('textarea').expandingTextarea('val', 'A long\n\nmultiline\n\n\nstring');

Afterward you will notice that the textarea has expanded to show the full val.

bgrins commented 11 years ago

Cool, looks like a nice solution to this problem. I wonder if adding a change handler would get the same effect. So instead of:

textarea.bind("input.expanding propertychange.expanding keyup.expanding", resize);

if we did:

textarea.bind("input.expanding propertychange.expanding keyup.expanding change.expanding", resize);

Would that solve the problem without needing the extra call?

Crisfole commented 11 years ago

Totally possible. I assume change.expanding is an event triggered manually in expandingTextarea? textarea and input don't fire change events when setting by val

bgrins commented 11 years ago

Oh, I was assuming that it would automatically fire. I see now that it doesn't fire when set with val(). I think your solution is probably better. I guess you could manually run:

$(this).val("something").trigger("input.expanding")

But I'm fine with adding this to the API to make it easier to remember.

Crisfole commented 11 years ago

I used to run it that way, but got sick of repeating myself.

bgrins commented 11 years ago

Closing this due to the updates in https://github.com/bgrins/ExpandingTextareas/pull/31. Not perfect, but you can now call:

$(this).val("something").change();