I have piece of javascript code, which should process when the browser window is resized.
resize.style.marginRight=(resize.offsetWidth-$(this)[0].offsetWidth)+'px'
Tryed this stuff (doesn’t work):
window.onresize = function(event) {
resize.style.marginRight=(resize.offsetWidth-$(this)[0].offsetWidth)+'px'
}
Full code available – http://jsbin.com/adelu3/2/ (see page source)
Html (generated by the script):
<div class="resizable-textarea">
<span>
<textarea class="resizable processed" cols="" rows=""></textarea>
<div class="resize"></div>
</span>
</div>
Thanks.
offsetWidthis a property of elements. In the callback code ofwindow.onresize,thisis aWindow, which doesn’t have an offsetWidth.What is
thissupposed to be? (Theonresizeevent is not present in the linked code.) If you want to read the window width, use$(window).width().If you want to read the width of some other (ancestor?) element that you had as
thisin the enclosing scope, you must either find that element by looking up from theresizeelement, or retain a reference to the other element in a closure, eg.:(nb.
$(this)[0]does precisely nothing.)