Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to disable textarea's multiline option?

Tags:

html

textarea

Of course, I can use standard html text box, but I need text area for some reason.

So, is it possible to disable textarea's multiline option?

like image 764
tesicg Avatar asked May 24 '12 06:05

tesicg


People also ask

How do I turn off text area?

When present, it specifies that the text area should be disabled. A disabled text area is unusable and the text is not selectable (cannot be copied). The disabled attribute can be set to keep a user from using the text area until some other condition has been met (like selecting a checkbox, etc.).

Is the multiline text input control?

The <textarea> tag defines a multi-line text input control. The <textarea> element is often used in a form, to collect user inputs like comments or reviews.

What is multiline in HTML?

It is used within a form, to allow users to input text over multiple rows. Here are the attributes of <textarea> tag − Attribute.


4 Answers

You can set the size of your textarea using the cols and rows attributes and make it non-resizable in CSS but you can't prevent the user from adding more than one line. Even if the area is just too small, a scrollbar will appear and let them write as many lines as they want.

If you really need to get just one line, I suggest setting the rows attribute to 1 and checking if the input has a single line with Javascript.

In html:

<textarea name="a"  cols="5" rows="1" ></textarea>

In CSS:

textarea{
    resize: none; 
    #You can use resize: horizontal if you just want to disable vertical resize
}

Still, I'd suggest using an <input type="text" ... /> Why do you want to avoid it?

like image 96
toniedzwiedz Avatar answered Oct 21 '22 07:10

toniedzwiedz


You can keep all the text on a single line by settings rows="1" on the <textarea>, like the other answers have suggested, and then applying the following CSS to the text area:

resize: none;
white-space: nowrap;
overflow-x: scroll;

This CSS will prevent the single row from wrapping, while still making the keeping the whole line visible by providing a scroll bar to navigate any text that overflows the visible area.

like image 30
allanlasser Avatar answered Oct 21 '22 05:10

allanlasser


Yes, it is possible using input event to remove all new-line characters every time a user inputs something.

<textarea oninput="this.value = this.value.replace(/\n/g,'')"></textarea>

Or in a more civilized way:

html

<textarea id="ta"></textarea>

js

document.getElementById('ta').addEventListener('input', function(e){
    this.value = this.value.replace(/\n/g,'')
});

Demo on JSFiddle

like image 8
Slavik Meltser Avatar answered Oct 21 '22 07:10

Slavik Meltser


You can use wrap="off" attribute in textarea to be displayed in a single line.

<textarea wrap="off"></textarea>
like image 5
mithz07 Avatar answered Oct 21 '22 06:10

mithz07