0

I have a textarea on my page, with resize:both;

I want to be able to apply a style to that textarea only when the user resizes it. Preferably I would like to be able to do this with CSS, but I dont think there is a pseudo-selector or method that would work with just pure CSS. Am I wrong?

Failing this, is there an event I can make use of in jQuery?

And what would be the best way of reseting the textarea to its original size? Would this be with jQuery again?

Many thanks.

Jimmery
  • 9,783
  • 25
  • 83
  • 157

3 Answers3

3

Without using any other library, creating own resize event for textarea could be done like this:

DEMO jsFiddle

$('textarea').on({
    mousedown: function(){
        $(this).data('size', $(this).width + '*' + $(this).height());
    },
    mouseup: function(){
        if($(this).data('size') !=  $(this).width + '*' + $(this).height())
            $(this).triggerHandler('resize');
    },
    resize: function(){
        console.log('textarea resized!');
    }
});
A. Wolff
  • 74,033
  • 9
  • 94
  • 155
  • 1
    although the jQueryUI resizable method is an excellent solution to this problem, I ended up using an adaptation of this code, so I'm going to mark you as correct. – Jimmery Feb 11 '14 at 14:58
  • 1
    @Jimmery I accept it. This is a nice solution. Unless you already use JqueryUI, i hope it would be a pain to load whole library for a single functionality. Anyway in future if you use it my answer might be helpful to you. – Shiva Avula Feb 11 '14 at 16:30
2

You need to first make the textarea resizable before issuing the resize event. You can do that by using jQuery UI resizable() and inside it you can call the resize event.

CSS

.resizing {
    background: #f2f2f2;
    border: 1px solid #4cf;
}

JS

$("textarea").resizable({ // Making it resizables triggers resize associated events
    resize: function() {
        $(this).addClass('resizing'); // Add style while resizing
    },
    stop: function() {
        $(this).removeClass('resizing'); // Remove style after resize finishes
    }
});

Here is a DEMO

Shiva Avula
  • 1,836
  • 1
  • 20
  • 29
0

you can try this..

$("#mytextarea").resizable({
    resize: function() {
        $(this).css("resize","both");
    }
});

http://jsfiddle.net/gR9UV/

Ashwini Verma
  • 7,477
  • 6
  • 36
  • 56