Skip to content

Instantly share code, notes, and snippets.

Last active October 10, 2020 16:53
What would you like to do?
Toggle amount of rows in text area.
//short version, the one in the answer
$('thetextarea').attr('rows', $('thetextarea').attr('rows')==1?5:1);
//short version, just using selectors once
var $textArea = $('thetextarea'); //declare a variable to avoid using the same jQuery selector twice.
$textArea.attr('rows', $textArea.attr('rows')==1?5:1);
//long version
var currentRows = $('thetextarea').attr('rows'); //obtain the current number of rows in the textarea
var newRows; //declare a variable to hold the new number of rows
if(rows == 1){ //if just one...
newRows = 5; // it should become 5
newRows = 1; //else, (not 1), become 1
$('thetextarea').attr('rows', newRows); //assign the new value to the rows attribute of the textarea
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment