Refer to the answer provided here:
Dealing with multiple lines of input in <input type="text"
By including the css property word-break: break-word;
, you can achieve the desired behavior you're looking for. After performing a quick test, it does demonstrate the expected outcome.
However, keep in mind that there are additional features that textarea
offers which input[type="text"]
cannot replicate. Nonetheless, it's a good starting point!
View DEMO
input{
height:500px;
width:500px;
word-break: break-word;
}
<input type="text">
Although applying this style will allow text to wrap to the next line upon reaching the right border, it won't enable using the return key to start a new line and the text will be vertically centered within the input field.
Should JavaScript be an option, rather than attempting to manipulate input
into behaving like a textarea
, the optimal solution is to display an actual textarea
, hide the text input
, and ensure both are synchronized through javascript. You can find an example fiddle:
http://jsfiddle.net/fen05zd8/1/
If jQuery is viable, consider dynamically converting your designated input[type="text"]
to a textarea
. During conversion, all pertinent attributes such as id
, value
, and class
can be copied over. Events will automatically link to the substituted textarea
either through event delegation or via class selectors.
This approach allows you to retain your current markup without altering it (if changing to textarea is not feasible). Simply inject a bit of Javascript / jQuery to mirror the functionality of textarea
using the actual element itself.
You can view a sample demo utilizing Javascript above.
Another demonstration incorporating jQuery can be found here: http://jsfiddle.net/abhitalks/xqrfedg2/
Here's a simplified snippet:
$("a#go").on("click", function () {
$("input.convert").each(function () {
var $txtarea = $("<textarea />");
$txtarea.attr("id", this.id);
$txtarea.attr("rows", 8);
$txtarea.attr("cols", 60);
$txtarea.val(this.value);
$(this).replaceWith($txtarea);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label>Name:</label><input id="txt1" type="text" />
<br /><br />
<label>Address:</label><input id="txt2" type="text" class="convert" value="Address here.." />
<hr />
<a id="go" href="#">Convert</a>