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?
It can be achieved by using JavaScript and jQuery. Method 1: Using JavaScript: To change the height, a new function is created which changes the style property of the textarea. The height of the textarea is first set to auto. This value makes the browser set the height of an element automatically.
To add the auto resize, just call the function addAutoResize() once your textareas are exposed in DOM.
The resize event fires when the document view (window) has been resized. This event is not cancelable and does not bubble.
The onresize event occurs when the browser window has been resized. Tip: To get the size of an element, use the clientWidth, clientHeight, innerWidth, innerHeight, outerWidth, outerHeight, offsetWidth and/or offsetHeight properties.
Chrome doesn't capture the resize event and that Chrome doesn't capture the mousedown, so you need to set the init state and then handle changes through mouseup:
jQuery(document).ready(function(){ var $textareas = jQuery('textarea'); // store init (default) state $textareas.data('x', $textareas.outerWidth()); $textareas.data('y', $textareas.outerHeight()); $textareas.mouseup(function(){ var $this = jQuery(this); if ( $this.outerWidth() != $this.data('x') || $this.outerHeight() != $this.data('y') ) { // Resize Action Here alert( $this.outerWidth() + ' - ' + $this.data('x') + '\n' + $this.outerHeight() + ' - ' + $this.data('y') ); } // store new height/width $this.data('x', $this.outerWidth()); $this.data('y', $this.outerHeight()); }); });
HTML
<textarea></textarea> <textarea></textarea>
You can test on JSFiddle
Note:
resize:vertical
to lock movement.For something more advanced you'd need to add other listeners, possibly a queue and interval scanners; or to use mousemove, as I believe jQuery resizable does -- the question then becomes how much do you value performance vs polish?
Update: There's since been a change to Browsers' UI. Now double-clicking the corner may contract the textbox to its default size. So you also may need to capture changes before/after this event as well.
A standard way to handle element's resizing is the Resize Observer api, available in all modern web browser versions.
function outputsize() { width.value = textbox.offsetWidth height.value = textbox.offsetHeight } outputsize() new ResizeObserver(outputsize).observe(textbox)
Width: <output id="width">0</output><br> Height: <output id="height">0</output><br> <textarea id="textbox">Resize me.</textarea>
If you need to deal with old versions of Chrome and Firefox (others untested), Mutation Observer can be used to detect the change of the style attribute.
function outputsize() { width.value = textbox.offsetWidth height.value = textbox.offsetHeight } outputsize() new MutationObserver(outputsize).observe(textbox, { attributes: true, attributeFilter: [ "style" ] })
Width: <output id="width">0</output><br> Height: <output id="height">0</output><br> <textarea id="textbox">Resize me.</textarea>
Documentation: https://developer.mozilla.org/en-US/docs/Web/API/Resize_Observer_API
Spec: https://wicg.github.io/ResizeObserver
Current Support: http://caniuse.com/#feat=resizeobserver
Polyfills: https://github.com/pelotoncycle/resize-observer https://github.com/que-etc/resize-observer-polyfill https://github.com/juggle/resize-observer
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With