7

I have an element input[type=text], I want to make it works as an textarea element. Means I need to have a bigger height of an input[type=text] element with showing multiple lines just like an textarea element. I have an issue that I can't take textarea in place of input[type=text], so I need a way where I can convert or make it work as textarea.

augustoccesar
  • 658
  • 9
  • 30
Vinay Patial
  • 113
  • 1
  • 3
  • 14
  • 2
    possible duplicate of [Multiple lines of input in ](http://stackoverflow.com/questions/6262472/multiple-lines-of-input-in-input-type-text) – James Thorpe Nov 20 '14 at 10:45
  • 2
    Any reason for why *" issue that I can't take textarea in place of input[type=text]"* ?? – Abhitalks Nov 20 '14 at 10:49
  • I'd asy it's not a dupe. That other question was answered by "use a textarea", which is specifically ruled out here. Not saying there may be any other solution, just that it's not a dupe. – Victor Nov 20 '14 at 10:53
  • @Victor Yeah. The second answer also pointed out that "it's not possible" though, which is what's likely to end up on here too – James Thorpe Nov 20 '14 at 10:54
  • I'd say @Victor is right, it isn't a dupe although the other thread seems to have an solution (haven't tested it though) http://stackoverflow.com/a/20376723/1811992 – web-tiki Nov 20 '14 at 10:54
  • 2
    Nice! That answer also happened two years after the question, so maybe there is a solution now we hadn't before. then I suggest answering the same here, maybe this time it gets accepted and everybody wins. – Victor Nov 20 '14 at 10:58
  • @Victor Fair enough. [Having looked](http://caniuse.com/#search=word-break), it does seem widely available – James Thorpe Nov 20 '14 at 10:59
  • Actually I am implementing "mailchimp-form" and they don't have the textarea field to add. – Vinay Patial Nov 20 '14 at 11:02
  • 1
    Even `word-break: break-word;` won't solve the problem. It only hard breaks for the wrap. Newlines will still be not registered. The only solution is to dynamically replace using javascript. That's why I asked what is the problem with textarea. – Abhitalks Nov 20 '14 at 11:04
  • @VinayPatial, can you add any javascript at all? Just CSS? – Victor Nov 20 '14 at 11:08
  • @victor, any solution will be acceptable except taking textarea in place input[type=text]. Yes if there's any way of converting it to textarea through javascript will be good, because from my point of view there's no css way of doing it. – Vinay Patial Nov 20 '14 at 11:11
  • @VinayPatial Is this an issue of a receiving end not catering for the data that a `textarea` produces (eg multiple lines etc), rather than not physically being able to put one on the page in the first place? – James Thorpe Nov 20 '14 at 11:12
  • In mailchimp they haven't provide any option for adding textarea to the form, so I have taken input[type=text] in place of it, but I want to make it work as textarea. – Vinay Patial Nov 20 '14 at 11:15
  • @VinayPatial, if we open the JavaScript can of worms, I think at some point the best solution is to put a textarea in there, and keep the text input hidden and in sync with the textarea. Assuming you need to keep that input in place to make whatever other bits of the application happy. – Victor Nov 20 '14 at 11:17
  • @victor I think I have to make it work through javascript. – Vinay Patial Nov 20 '14 at 11:20

5 Answers5

10

As answered here:

Multiple lines of input in <input type="text" />

It turns out that adding the css property word-break: break-word; makes it behave a bit more as you want it. I did a quick test and it does work.

Buyer beware, there will be other features textarea does that input[type="text"] can't have. But it's a start!

DEMO

input{
    height:500px;
    width:500px;
    word-break: break-word;
}
<input type="text">

This will only allow the text to flow to the next line when it hits the right border but it won't let you use the return key to start a new line and the text is verticaly centered in the input.


If JavaScript is an option, as much fun as it is to try and twist poor input's arm until it behaves as a textarea, the best solution to your problem is to display an actual textarea, hide the text input and keep both in sync with javascript. Here is the fiddle:

http://jsfiddle.net/fen05zd8/1/


If jQuery is an option, then you could convert your target input[type="text"] to textarea on the fly. While converting, you could copy all relevant attributes like id and value and class. Events will automatically point to the replaced textarea bound using event delegation or via class selectors.

This way you won't have to change your existing markup (as you said changing to textarea is not possible for you). Just inject a little Javascript / jQuery and viola you get the functionality of textarea using actual textarea.

One sample demo using Javascript is above. Another sample demo using jQuery is here: http://jsfiddle.net/abhitalks/xqrfedg2/

And a simple 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>
Community
  • 1
  • 1
Victor
  • 9,210
  • 3
  • 26
  • 39
  • http://stackoverflow.com/questions/27037548/how-can-make-an-inputtype-text-element-to-work-as-textarea#comment42595670_27037548 – Abhitalks Nov 20 '14 at 11:05
  • @abhitalks, that's true, newlines don't work right. Ah well, can't have everything :) – Victor Nov 20 '14 at 11:07
  • It doesn make it pretty wierd but it lets the text flow to the next line for me http://jsfiddle.net/webtiki/d142ruk4/ – web-tiki Nov 20 '14 at 11:10
  • @web-tiki problem is, as abhitalks pointed out, you can't hit return to start a new line etc – James Thorpe Nov 20 '14 at 11:11
  • @web-tiki: If the Op is using jQuery, it becomes even easier. You may want to use this: http://jsfiddle.net/abhitalks/xqrfedg2/ – Abhitalks Nov 20 '14 at 11:36
  • @abhitalks yes, that seems much better! can you edit that in the answer? – web-tiki Nov 20 '14 at 11:45
  • Sorry, while that's interesting as an edge case of text input, it doesn't really work as a text area would. – Paul Nov 20 '14 at 11:45
3

$('input[type="button"]').click(function(e) {
  $('input[type="text"]').val($('#source').html());  
});
#source {
  border: 1px solid #ccc;
  height: auto;
  margin-top: 10px;
  min-height: 10px;
  overflow: auto;
  padding: 5px;
  width: 106px;
  word-break: break-word;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" name="text" />
<div id="source" contenteditable=""></div>

<input type="button" value="Send" />

This would be the HTML, the only downside to this is that the input would submit HTML. Take this for example..

Lorem ipsum Dolores amet

would submit like this: Lorem ipsum <br> Dolores amet

Here's a codepen: http://codepen.io/pacMakaveli/pen/MYYOZW

Vlad
  • 902
  • 4
  • 14
0

Why don't you use Textarea tag like

​<textarea id="txtArea" rows="10" cols="70"></textarea>

The "input" tag doesn't support rows and cols attributes. This is why the best alternative is to use a textarea with rows and cols attributes. You can still add a "name" attribute and also there is a useful "wrap" attribute which can serve pretty well in various situations.

There is no meaning for giving height to input tag for reference

More than 1 row in <Input type="textarea" />

Community
  • 1
  • 1
Swapnil Motewar
  • 1,080
  • 6
  • 12
0

You can't make an input[type=text] work as a <textarea> . Because only HTML form element that's designed to be multiline is textarea.

user3351229
  • 97
  • 1
  • 2
0

Would something like this http://jsfiddle.net/710u7qns/ work? You'd need to treat "Enter" as new line on the back end.

html:

<form id="frm">
    <input name="ip" type="text" id="it" style="display:none;"></input>
    <textarea id='ta'></textarea>
    <input type="submit" id="btn">submit</button>
</form>

JS:

document.getElementById("ta").addEventListener("keyup", function(e){
    document.getElementById("it").value = document.getElementById("it").value + e.key;
});
artm
  • 8,554
  • 3
  • 26
  • 43