I use eventlistener to detect the rows in textarea, but when I press backspace the if condition doesn’t work. How can I improve this?
$('textarea').on('input', function() {
// Get the scroll height of the textarea
let taHeight = $('textarea').scrollHeight;
let numberOfLines = Math.floor(taHeight / 16);
height(numberOfLines);
})
function height(numberOfLines) {
console.log(numberOfLines);
if (numberOfLines == 2) {
$('textarea').removeClass('row-2 row-3').addClass('row-2');
} else if (numberOfLines == 3) {
$('textarea').removeClass('row-2 row-3').addClass('row-3');
} else {
$('textarea').removeClass('row-2 row-3');
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<textarea></textarea>