The attribute rows
specifies the visible number of lines in a text area, what you are trying to do has nothing to do with your problem. To limit the number of characters in the widget textarea
, you can limit the number of characters using the attribute maxlength
in the element textarea
.
In this example I limited the number of characters in maximum 3:
const txtArea = document.getElementById('txtArea');
txtArea.addEventListener('keydown', (event) => {
if(event.code == 'Enter') {
alert("O BOTÃO ENTER FOI CLICADO! QUEBRANDO A LINHA...");
}
});
<textarea id="txtArea" maxlength="10" rows="10"></textarea>
About breaking the line when you click the "enter" button, this feature does not need to be implemented, this is already pre-programmed. See the animation below, using the code above:
To prevent line breaking you must add the preventDefault()
in the event
, thus:
const txtArea = document.getElementById('txtArea');
txtArea.addEventListener('keydown', (event) => {
if(event.code == 'Enter') {
alert("O BOTÃO ENTER FOI CLICADO! QUEBRANDO A LINHA...");
event.preventDefault();
}
});
<textarea id="txtArea" maxlength="10" rows="10"></textarea>
The attribute
rows
is used to define how many lines(height in lines) thistextarea
when rendering, it would not be better to set a character limit in thetextarea
that meets your need?– Aesir
I understand... I have, but he is limited by the maximum number of characters, but he allows me to break as many lines as I want. I want to limit to 3 lines at most.
– Rafael Veloso
The textarea will exceed 3 lines if your maxlength extrapolate the 3 lines
(maxlenght="180")
, you will need to limit maxlength to a maximum of 3 lines(maxlength="150")
for example or increase the lines to fit the 180.– LeAndrade
You want the user not to be able to give
enter
to break the line within the textarea, or you want the content to not exceed more than 3 lines within the textarea?– hugocsl
content does not exceed 3 lines within the textarea
– Rafael Veloso