javascriptjquery

How do I simplify this?


I'm new to JS, and I'm self-learning. I'd like to know how do I simplify this? This kind of block is repeated too many times in my script.

$('.itemlist').on('focus', 'textarea.remarks', function (){
        $(this).animate({height: '50px'},400);});
$('.itemlist').on('blur', 'textarea.remarks', function (){
        $(this).animate({height: '15px'},400);});

Cheers.


Solution

  • You could also do something like this:

    $('.itemlist').on({
        focus: function(){
            $(this).animate({ height: '50px' }, 400);
        },
        blur: function(){
            $(this).animate({ height: '15px' }, 400);
        }
    }, 'textarea.remarks');
    

    Not to say that it is any simpler, but it looks pretty. :)