gtilx
gtilx

Reputation: 2055

How to force single line in a HTML textarea element?

Is there a way to make a HTML textarea element to allow only one line?

I want to do this because I will have a button to expand it, but if user is allowed to enter multiple lines when the textarea is not expanded (only 1 row) it doesn look very nice, and if I use a textbox for the not expanded state, it looks weird when I replace it with the textarea and expand it with animation.

Upvotes: 5

Views: 11700

Answers (3)

Blake
Blake

Reputation: 764

Not in plain HTML, however, if you use JavaScript you could do it.

Upvotes: -1

Blair McMillan
Blair McMillan

Reputation: 5349

As far as I know you'd need to use javascript to monitor and prevent the enter key.

Upvotes: 1

BrunoLM
BrunoLM

Reputation: 100341

No.

You can stack the elements, then show the textarea first and remove the textbox right away. I guess it would be the workaround.

Example on how to stack elements on jsFiddle.

Upvotes: 1

Related Questions