I want my <textarea>
to auto resize while writing a comment on my wordpress site.
I have tried using js and jquery solutions from different tutorials but none of them seem to work. At first I thought my script.js wasn’t linked properly but i could find it in my source code. I have tried making the <textarea>
into <div contenteditable= true>
but wordpress does not detect the text for some reason, so it doesn’t let me post comments with this solution
2
Answers
The best would be to find that reason. But if you don’t know how or don’t want to, there is a quick "hack" that can be done here.
You will leave the
textarea
in place, but will add a class to hide it.Then, you will add the
div contenteditable
right next to it.And finally, the below script will litterally copy the text content from the
div
to thetextarea
in real time.So on submit, the textarea will have some content just like before.
It will work for one or more "replacements".
Type in the blue div below… 😉
If you want the textarea to resize vertically as the user types in the textarea you can use the
scrollheight
from the textareaoninput
event to set the height of the textarea.See example below…
Here is a jsFiddle version… https://jsfiddle.net/joshmoto/7wv6eo3q/