0

I use MultiDatesPicker calendar where the user can select some dates. A textarea (with readonly tag) object filled automatically with dates, while the user selects some of them and is working fine. What I liked to do now is to make height of textarea equal to height of the text within it (dates from Multidatespicker). Starting from 1 row.

I tried this answer (2nd answer) Creating a textarea with auto-resize (working: jsfiddle) but didn't work for my case

HTML

<div class="pick-multi-dates"></div>
<textarea rows="1" id="input-multi-dates" class="input-multi-dates"></textarea>

JQUERY

    $('.pick-multi-dates').multiDatesPicker({
        minDate: 0,
        altField: '#input-multi-dates',
        onSelect: function() {
            $('.input-multi-dates').css('height', 'auto' );
            $('.input-multi-dates').height( this.scrollHeight );
        }
    });

CSS

.input-multi-dates {
    overflow-y: hidden; /* prevents scroll bar flash */
    padding-top: 1.1em; /* prevents text jump on Enter keypress */
}

Thank you

Community
  • 1
  • 1
user3670128
  • 47
  • 1
  • 8

2 Answers2

1

Try this:

$('.pick-multi-dates').multiDatesPicker({
        minDate: 0,
        altField: '#input-multi-dates',
        onSelect: function() {
            var text = document.getElementById('input-multi-dates');
            text.style.height = 'auto';
            text.style.height = text.scrollHeight+'px';
        }
    });
Stargazer
  • 478
  • 2
  • 9
1

Or your fixed jQuery version:

$('.pick-multi-dates').multiDatesPicker({
    minDate: 0,
    altField: '#input-multi-dates',
    onSelect: function() {
        var $area = $('.input-multi-dates');
        $area.css('height', 'auto' );
        // 'this' within onSelect function refers to the associated input field
        // see: http://api.jqueryui.com/datepicker/#option-onSelect
        $area.css( 'height', $area[0].scrollHeight );
        // according to jQuery doc append 'px' is not neccessary: When a number is passed as the value, jQuery will convert it to a string and add px to the end of that string.
        // see: http://api.jquery.com/css/
    }
});
Zaragoli
  • 676
  • 4
  • 7