JavaScript:
function adjustRows() {
var value = document.getElementById("test1").value.length;
if (value <= 18) {
value.rows = 1;
} else if (value > 18 && value < 36) {
value.rows = 2;
} else if (value > 36 && value < 54) {
value.rows = 3;
}
}
HTML:
<input type="textarea" id="test1" style="overflow:auto" rows="1" cols="18" onkeypress="javascript:adjustRows();">
3 inquiries:
- How can I replace these conditional statements with a for or while loop?
- The maximum size of the field should be 18 but currently it doesn't work exactly at 18 even though I set
cols="18"
. - Is "onkeypress" the best method to use? I use this event to delete the value inside a textbox by pressing delete or backspace keys, which are not captured by "keypress". This means it doesn't dynamically adjust the rows.