As a server-side developer, I am venturing into the world of vanilla JS to expand my skillset.
My current challenge involves creating a form where textareas
are initially hidden behind button clicks and resize as more text is entered. This task is pushing the boundaries of my expertise, but I'm determined to tackle it on my own for learning purposes.
Here's my progress so far.
Picture a forum where users post content with a "reply" button underneath. Clicking this button reveals a basic textarea
for typing responses. Here's the simple JavaScript code I used:
var replyBtns = document.querySelectorAll('[id=rep]');
for(var i = 0; i < replyBtns.length; i++) {
replyBtns[i].addEventListener('click', function(e) {
e.preventDefault();
var textarea = document.getElementById("reply-message");
if (textarea) { textarea.parentNode.removeChild(textarea); }
var replyBox = document.createElement('textarea');
replyBox.setAttribute('id', 'reply-message');
replyBox.setAttribute('placeholder', 'Reply');
this.parentNode.insertAdjacentElement('beforeend', replyBox);
}, false);
}
This JavaScript snippet creates a text area in the HTML, paired with the following CSS:
textarea#reply-message {
display: block;
border: none;
color:#306654;
font-size:120%;
padding: 5px 10px;
line-height: 20px;
width:550px;
height: 20px;
border-radius: 6px;
overflow: hidden;
resize: none;
}
The system works smoothly.
Moving forward, my next goal was to make the textarea
resize dynamically as the text surpasses its initial size. To achieve this, I followed these steps:
Retrieve the content within the
textarea
Create an invisible clone
div
Style the clone to mimic the
textarea
's dimensions and typographyCopy the content into the clone
Determine the height of the clone
Adjust the height of the
textarea
accordingly
This approach capitalizes on a div
element's inherent ability to expand based on its content height. Credit goes to this blog post for enlightening me on this technique.
Below is the JavaScript implementation of the aforementioned method:
let txt = document.getElementById('reply-message'),
hiddenDiv = document.createElement('div'),
content = null;
hiddenDiv.classList.add('hiddendiv');
document.body.appendChild(hiddenDiv);
txt.addEventListener('keyup', function () {
content = this.value;
hiddenDiv.innerHTML = content + '\n\n';
this.style.height = hiddenDiv.getBoundingClientRect().height + 'px';
}, false);
Define hiddendiv
as follows:
.hiddendiv {
position: absolute;
left: -9999px;
visibility: hidden;
white-space: pre-wrap;
width: 550px;
height: 20px;
font-size: 120%;
padding: 5px 10px;
word-wrap: break-word;
overflow-wrap: break-word;
}
However, I need to ensure that this JavaScript snippet only executes once the textarea
actually exists.
Currently, it runs upon page load, without any impact since textareas
have not yet been created. How can I make sure it activates only after their creation? As a novice in JavaScript, I'm struggling with this concept. Any guidance would be greatly appreciated.