Skip to content

Instantly share code, notes, and snippets.

@glpzzz
Last active October 10, 2020 16:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save glpzzz/762afe32e8c245216869e9b1bd0aaac0 to your computer and use it in GitHub Desktop.
Save glpzzz/762afe32e8c245216869e9b1bd0aaac0 to your computer and use it in GitHub Desktop.
Toggle amount of rows in text area.
//short version, the one in the answer
$('thebutton').click(function(){
$('thetextarea').attr('rows', $('thetextarea').attr('rows')==1?5:1);
});
//short version, just using selectors once
$('thebutton').click(function(){
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
$('thebutton').click(function(){
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
}else{
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