The current versions of Firefox and Chrome include a drag handler to resize a <textarea>
box. I need to capture the resizing event, I thought it would be easy with jQuery's resize()
event, but it doesn't work!
I have also tried the normal onResize
event, but the result is the same. You can try it on JSFiddle.
Is there a way to capture it?
FireFox now supports MutationObserver events on textareas and this seems to work quite well. Chrome sadly still needs a workaround.
Based on the other answers on this page, here's a refactored and updated version, that triggers a window resize event when a
textarea
is resized.I've also added an event listener for the mouse leaving the window which is needed in an iFrame to detect when the
textarea
becomes larger than the frame.In IE9 and above we can do the same without jQuery.
You need to first make the textarea resizable before issuing the resize event. You can do that by using jQuery UI resizable() and inside it you can call the resize event.
Check working example at http://jsfiddle.net/HhSYG/1/
Resize event doesn't exist for textarea.
The resizeable jQueryPlugin doesn't look native, so we must use alternative.
One way to emulate it is to use the mousedown/click event. If you want real-time event triggering, you can do it like this:
Updated november 11, 2013:
Demo : http://jsfiddle.net/gbouthenot/D2bZd/
thanks to MoonLite - Your script is working fine, but sometimes, if You do a quick increasing-resize the mouse pointer is outside the textarea on mouseup and the wished function is not triggered. So I added a mouseup event on the containing element to make it work reliable.
.
'I mixed vol7ron's answer up a bit, and just replaced the "Resize Action Here" with a simple trigger of the normal "resize" event, so you can attach whatever you want to happen to the resize event "as usual":
I added the mousemove event so the resizing also fires while dragging the mouse around while resizing, but keep in mind that it fires very often when you move the mouse around.
in this case you might want to put a little delay in actually triggering or handling the resizing event, e.g. replace the above:
with:
example usage, make 2nd textarea grow and shrink with the first one:
A new standard for this is the Resize Observer api, available in Chrome Dev 54 behind the experimental web platform features flag.
Alternatively, Mutation Observer can be used to detect the change of the style attribute in Firefox and Chrome 54.
Resize Observer
Spec: https://wicg.github.io/ResizeObserver
Polyfill: https://github.com/pelotoncycle/resize-observer
Chrome Issue: https://crbug.com/612962
Chrome Flag: chrome://flags/#enable-experimental-web-platform-features
Firefox Issue: https://bugzil.la/1272409
Safari Issue: http://wkb.ug/157743