A while ago I've solve an issue for someone that wanted his textarea to grow. I've made a function that listens to the scroll
and keyup
events of the area and recalculates the number of rows. I wanted to use the code in another project, but there's a problem. The textarea's are not know To solve this, I'm using live
instead of bind
, so that future area's will be bound as well.
Now I'm finding that the live
executes a lot slower than a bind
. I've created a simplified example on jsFiddle. The upper textarea behaves as I want, but newly added ones flicker due to the late signaling (I'm using Chrome).
How can I make the The problem is that the live
as fast as the bind
?scroll
can't be used with a live
statement. Is there a way to enable scroll
for live
? Is there maybe a jQuery event that signals me that a new TextArea has been added, so I can use a bind to add the scroll
on the newly created element?
I'm looking forward to your ideas.
EDIT: Changed link to the code. Removed scrollingCode. Added another button to create a different textarea. The problem has to do with 'scroll'. It doesn't fire.
Clarification: I will not know what function will create the textarea's. I see flickering on the dynamically added boxes in Chrome.
For future readers:
In jQuery 1.3.x only the following JavaScript events (in addition to custom events) could be bound with .live():
click, dblclick, keydown, keypress, keyup, mousedown, mousemove, mouseout, mouseover, and mouseup
. As of jQuery 1.4 the .live() method supports custom events as well as all JavaScript events that bubble. As of jQuery 1.4.1 even focus and blur work with live (mapping to the more appropriate, bubbling, events focusin and focusout). As of jQuery 1.4.1 the hover event can be specified (mapping to mouseenter and mouseleave, which, in turn, are mapped to mouseover and mouseout).
Limit the minimum execution interval time of ONSCROLL event The onscroll event trigger interval time is around 10~20ms when we scrolling the mouse wheel.
The scroll event does not bubble up. Although the event does not bubble, browsers fire a scroll event on both document and window when the user scrolls the entire page.
You can check if window. scrollY (the number of pixels the window has scrolled vertically) is equal to 0 . If you want to check if the window has been scrolled to its leftermost, you can check if window. scrollX (the number of pixels the window has scrolled horizontally) is equal to 0 .
The answer is simple. scroll
is what prevents the flickering, because it fires at the very first moment of resize. But scroll
has no effect with live
(because it doesn't bubble), so your newly created textareas will be resized on keyup
but it fires later (thus the flickering).
Update: Of course I can even solve your problem. You just need to ask :) [Demo]
$('textarea.autoresize').live('keyup', function() {
var el = $(this);
if (!el.data("has-scroll")) {
el.data("has-scroll", true);
el.scroll(function(){
resizeTextArea(el);
});
}
resizeTextArea(el);
});
The point is, it mixes live
with bind
. The keyup
event, which fires on all elements (because of live
), adds the unique scroll
event conditionally.
Update 2: Oh, and by the way your whole resizing code can be better written as:
// resize text area (fixed version of Pointy's)
function resizeTextArea(elem) {
elem.height(1); elem.scrollTop(0);
elem.height(elem[0].scrollHeight - elem[0].clientHeight + elem.height())
}
Try this (JSFiddle):
$('#Add').click(function(){
var id = "newtextarea"+Math.floor(Math.random()*1000);
$('#pane').append($('<textarea class="new" rows="1" cols="40" id="'+id+'"></textarea><br/>'));
$('textarea:last').focus();
bindAgain(id);
});
//inital resize
resizeTextArea($('#tst'));
//'live' event
$('textarea.new').bind('keyup scroll', function() {
resizeTextArea($(this));
});
function bindAgain(id)
{
$('#'+id).bind('keyup scroll', function() {
resizeTextArea($(this));
});
}
Basically, it rebinds the event using a dynamically created ID. Not as elegant as karim79's solution, but it works.
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