1

Suppose i have a textarea with the size of a normal textbox, how do i make it stretchable when i press enter or crtl+enter to make a new line for user to type.

For example, i type something in textarea in the first line, then i press enter and the textarea will expand a new line for me to enter data.

<td>
    <textarea rows="5" cols="50" name="description[]">
        <?php echo $row[ 'description']; ?>
    </textarea>
</td>

Above is the textarea that i mentioned.

Arturs
  • 1,258
  • 5
  • 21
  • 28
nickee
  • 33
  • 1
  • 6

5 Answers5

1

You will have to use javascript. Specifically, you could use a javascript framework called jQuery. Check out this answer: Auto expand a textarea using jQuery

Community
  • 1
  • 1
htxryan
  • 2,871
  • 2
  • 21
  • 21
1

You could try some jQuery:

$("textarea").keyup(function(e) {
    if(e.keyCode == 13) //13 is the ASCII code for ENTER
        {
            var rowCount = $(this).attr("rows");
            $(this).attr({rows: rowCount + 5});
        }
});
hammus
  • 2,602
  • 2
  • 19
  • 37
  • http://stackoverflow.com/a/5346855/2716915 This work for me :) Though it's a long code, but i guess i understand the workflow.Thank to you all anyway :) – nickee Aug 29 '13 at 02:07
0

Alternatively - you could use a div with contenteditable set true.

<td>
  <div id="description[]" contenteditable="true">
    <?php echo $row['description']; ?>
  </div>
</td>
<input name="description[]" type="hidden" value="">

Then add a JS function to the form submit (assuming this is for a form) that sets the value of the hidden input to the inner text/content of the div

function updateText() {
  content = document.getElementById('description[]').textContent || document.getElementById('description[]').innerText;
  document.querySelector('[name="description[]"]').value = content;
}
Shylo Hana
  • 1,792
  • 13
  • 10
0

Check out this auto grow jQuery plugin - https://github.com/akaihola/jquery-autogrow

Matt Millican
  • 4,044
  • 4
  • 38
  • 55
0

I used like this:

Jquery:

$('#parent-block-for-textarea').on('keyup', 'textarea.autoexpand', function(ev) {
        var t = ev.target;
        t.style.height = 'auto';
        t.style.height = (t.scrollHeight) + 'px';
});
//this also works on dynamically added textarea.

HTML:

<textarea class="autoexpand"></textarea>

Without using jquery:

<textarea onkeyup="this.style.height='auto';this.style.height=(this.scrollHeight)+'px'"></textarea>
vusan
  • 5,221
  • 4
  • 46
  • 81