Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make input element (type="text") handle multiple lines of text

I've created a form with an input, but the box only handles text in a single row. I would like to style it so that the input field is similar to that of Twitter's, where the box itself is multiple rows:

twitter-input

And it also expands when you hit enter:

enter image description here

This is currently what I have:

<form name="userForm">
  <input type="text" id="userInput" name="userInput" placeholder="Enter text here.">
  <button type="submit" id="button">Submit</button>
</form>

I've styled the button and the input, but haven't done anything to change its shape, so it's at default. What do I have to tweak to achieve this?

like image 418
TheRealFakeNews Avatar asked Feb 21 '16 21:02

TheRealFakeNews


People also ask

Which form element can take multi line input?

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.

Which is used for multi line text input control?

Web apps often need forms for user interactivity, and this requires us as developers to use form controls such as text input boxes, dropdowns, checkboxes, and so on. One frequently used form control is textarea , which is used to get multi-line input from a user.

Which input control is used for multi line data collection?

The TEXTAREA element creates a multi-line text input control.

What form element is used in creating multi line text field?

<textarea>: The Textarea element. The <textarea> HTML element represents a multi-line plain-text editing control, useful when you want to allow users to enter a sizeable amount of free-form text, for example a comment on a review or feedback form.


2 Answers

You need to use an HTML <textarea> element.

From MDN:

<textarea>

The HTML <textarea> element represents a multi-line plain-text editing control.

Using <input> for multiline text is not possible natively and, if hacked, would be invalid HTML.

HTML5 spec:

4.10.5.1.2 Text (type=text) state and Search state (type=search)

The input element represents a one line plain text edit control for the element's value.

(emphasis mine)


Twitter input box

You mention you want the textarea to resemble Twitter's (auto-resize / no scrollbar). Consider this option and the following SO posts:

Autosize

A small, stand-alone script to automatically adjust textarea height.

  • Is it possible to hide the scroll bar on an HTML textarea element?
  • Remove scrollbars from textarea
  • It is possible to expand a textarea only with CSS?
  • Creating a textarea with auto-resize
  • is there a way to get a textarea to stretch to fit its content without using php or javascript?
like image 175
Michael Benjamin Avatar answered Oct 06 '22 19:10

Michael Benjamin


Use TextArea.Alter rows and columns attribute according to requirement.

<textarea class="input" rows="10" cols="10">Some text here</textarea>

To add the hover effect and change the box size , use css. Assign a normal height and width, and change that on focus.

.input:focus {
    height:300px;
}
.input{
    width:500px;
    height:200px;
}
like image 32
Abdul Basit Avatar answered Oct 06 '22 19:10

Abdul Basit