What I’m trying to do is this:
You click in a textarea, and it’ll expand to 100px. Normally it’s at 50px. If you click outside (or trigger the blur event after clicking in the textarea…) it should go back to it’s normal 50px height.
If you trigger the change event by entering something into the textarea, I want to be able to click on the submit button without it triggering the blur (to move it back to 50px).
Am I on the right track?
var expandTextarea = function() {
$('.js-textarea-slide').on('click', function(e) {
var typed = false;
$(this).change(function() {
typed = true;
});
$(this).animate({
height: '100'
}, 0);
});
$('.js-textarea-slide').blur(function(typed, e) {
if (!typed) {
alert('yo');
return false;
} else {
$(this).animate({
height: '50'
}, 0);
}
});
};
http://jsfiddle.net/khE4A/4/
Note that this follows the logic you described in your question, not what your code was trying to do. Anyway, a few important changes:
Your change event would be bound every time the textarea was clicked instead of once. For example, if you clicked on the textarea 3 times, you would bind the event 3 times instead of just the 1 time required.
Also, the part that actually made the code broken was that typed was out of scope of the blur handler. Giving a callback a parameter with a certain name does not pull that variable into scope. In fact, it would mask it if the variable had been in a previously accessible scope.
Another [pedantic] thing:
The function wrapping is unnecessary. As expandTextarea does not use
this, you can use the function directly:Anyway, given the description of the problem in your question, I believe what you’re looking for is: http://jsfiddle.net/khE4A/2/