Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make a slide down textarea in HTML

I want to make an HTML table with a lot of text input areas (textarea) that, when clicked on, expand downwards. Right now, when the textarea element gets clicked it expands fine, but it messes up the table layout in the process.

I want something like this enter image description here

Instead of this ugly thing I'm getting now enter image description here

Here is my current code

like image 251
DanielTA Avatar asked Dec 19 '25 04:12

DanielTA


2 Answers

Instead of manually messing with the CSS values, simply toggle a class. This allows you to easily play with absolute positioning without having messed-up JS. See demo here: http://jsfiddle.net/8Qa3C/1/

Instead of your current code, use that:

$(document).ready(function() {
    $('[id^="txt"]').focus(function() {
        $(this).addClass('expand');
    });
    $('[id^="txt"]').blur(function() {
        $(this).removeClass('expand');
    });
});

Then you can have some simple CSS like this:

.expand {
    box-shadow: 3px 3px 5px 2px gray;
    height: 150px;
    position: absolute;
    top: 5px;
    z-index: 10;
}

I also added a position: relative; to td.

like image 160
Florian Margaine Avatar answered Dec 20 '25 17:12

Florian Margaine


jsFiddle Demo

You should use positioning to accomplish this. Using position:absolute will remove the textarea from the flow of the document and allow it to "pop out" as your animated gif shows.

In order to have the positioning line up, you will also want to set the td to be position:relative in order to adjust the textarea to align to the td padding. A zindex will help to set it above the content as well.

td > textarea:focus{
 position:absolute;
 top:5px;
 z-index:1;
}
td{
 position:relative;
}

For added effect, you can animate the height change

$(this).animate({height:"150px"});
like image 30
Travis J Avatar answered Dec 20 '25 18:12

Travis J



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!