ChatGPT解决这个技术问题 Extra ChatGPT

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

I have this text input in a form:

<input type="text"
       cols="40" 
       rows="5" 
       style="width:200px; height:50px;" 
       name="Text1" 
       id="Text1" 
       value="" />

I am trying to get it to take multiple lines of input. The width and height make the box to be bigger, but the user can enter text all (s)he wants yet it fills one line only.

How do I make the input more like a textarea?


l
lambda

You need to use a textarea to get multiline handling.


And the textarea tag can't be self-closing. <textarea \> is invalid.
@alexH irrespective of textarea, that's the wrong slash. Original answer had self-closing, but at least it was the correct slash.
@Adam I know, but i can't edit i any more. And i don't want to remove it, because in my opinion the self-closing part is important.
Yes, but, textarea doesn't support the pattern attribute. So darn.
what I don't like about this is that in JQuery, you can't set textarea value using val(). You have to append to it. :(
S
Sté

It is possible to make a text-input multi-line by giving it the word-break: break-word; attribute. (Only tested this in Chrome)


Thanks! I noticed that Chrome was allowing multiple lines for inputs, which I totally didn't want to, and the reason was word-break which was inherited from the body element
Looks good in Chrome 39 and Safari 8.0.2, but not Firefox 34 in my brief testing. :( jsfiddle.net/msybs9g7
This solution is outdated and not supported in most browsers anymore. Use


Thanks. I modified this and used it in my react textarea
P.S you want to add overflow: hidden to the textarea, or else it will add the extra height few letters before the actual needed one.
M
Matías Fidemraizer

Check this:

http://www.w3.org/TR/html401/interact/forms.html#h-17.7

The TEXTAREA element creates a multi-line text input control


O
Osanda Deshan

You should use textarea to support multiple-line inputs.

<textarea rows="4" cols="50">
Here you can write some text to display in the textarea as the default text
</textarea>

M
MarsAndBack

Use <div contenteditable="true"> (supported well) with storing to <input type="hidden">.

HTML:

<div id="multilineinput" contenteditable="true"></div>
<input type="hidden" id="detailsfield" name="detailsfield">

jQuery:

$("#multilineinput").on('keyup',function(e) {   
    $("#detailsfield").val($(this).text()); //store content to input[type=hidden]
});
//optional - one line but wrap it
$("#multilineinput").on('keypress',function(e) {    
    if(e.which == 13) { //on enter
        e.preventDefault(); //disallow newlines     
        // here comes your code to submit
    }
});

M
MarsAndBack

If you're using React, you could use the 3rd-party UI library MUI. It has a custom proprietary <Input> element with a multiline option.

As LSE commented, ultimately it gets rendered as <textarea>.

  <FormControl>
    <InputLabel htmlFor="textContract">{`textContract`}</InputLabel>
    <Input
      id="textContract"
      multiline
      rows="30"
      type="text"
      value={props.textContract}
      onChange={() => {}}
    />
  </FormControl>

https://material-ui.com/components/text-fields/#multiline


From the documentation : The multiline prop transforms the text field into a <textarea> element.. So even though it looks like an <input>, it's rendering a <textarea>.
T
ThePrince

If you don't need to edit it, you can use a

text here

It will expand with the text.


This would remove the input element completely; doesn't help at all.