3

I have a input (html element) and sometimes the input (text) from the user is larger then the width of the input (html element) is there a way to make the input (html element) auto grow when the user is typing?

Obs. Better if made only with css

-------------------------------------EDIT 1-----------------------------------

I've made a JSfiddle, and as you can see the text is cropped when you enter to much text. I'm now trying to make it work with <span contenteditable="true" >

4 Answers4

6

If you need a no javascript/jquery solution I would recommend you to use contenteditable div instead of input element

Here's an example https://jsfiddle.net/sohelansari/631uzco9

Or if you want to use input element only, then unfortunately a pure css solution is not possible. You can write a jquery plugin, may be something like this -> Is there a jQuery autogrow plugin for text fields?.

Community
  • 1
  • 1
shinobi
  • 2,511
  • 1
  • 19
  • 27
1
<span contenteditable="true" style="display: inline-block; border: solid 1px black; min-width: 50px; max-width: 200px"></span>
bootle
  • 23
  • 5
  • 1
    Hi bootle; your code might be correct, but with some context it would make a better answer; for example, you could explain how and why this proposed change would resolve the questioner's problem, perhaps including a link to the relevant documentation. That would make it more useful to them, and also more useful to other site readers who are looking for solutions to similar problems. – Vince Bowdren Aug 31 '16 at 14:09
1

Try adding oninput="this.size = this.value.length" to your input tag.

<input
  type="text"
  oninput="this.size = this.value.length"
/>
Zsolt Meszaros
  • 21,961
  • 19
  • 54
  • 57
Joseph Abbey
  • 333
  • 3
  • 7
  • But how does it skips to the next line? after adding an specific width, this doesnt seem to fix the problem. – Non404 May 02 '23 at 12:59
-1
    function copy_data(val){
     var a = document.getElementById(val.id).value
     document.getElementById("text_to").value=a
    }    
</script>
</head>
<TEXTAREA ID="text_from" style="height:100px; width:600px;">

</TEXTAREA>

<TEXTAREA ID="text_to" style="height:100px; width:600px;">

</TEXTAREA>
<button onclick=copy_data(text_from)>Copy</button>