Once you interact with the button
, the textarea
automatically reverts back to its original height
, causing the click event to be missed. To prevent this, you can try to keep the textarea
at its expanded height even after losing focus, or introduce a slight delay so that the button is clicked first.
An effective method would be to use JavaScript's mouseover
event to increase the height of the textarea when hovering over the button.
After clicking the button, you can then revert the textarea back to its initial height.
Give this workaround a try and see if it resolves your issue!
HTML:
<textarea id="text">test</textarea> <br/>
<button id="button">Change</button>
CSS:
textarea{
height: 100px; /* initial height */
}
textarea:focus {
height: 200px; /* expanded height */
}
JavaScript:
document.querySelector('#button')
.addEventListener('click', function () {
document.querySelector('#text').value += 'changed';
document.querySelector('#text').style.height = '100px';
});
document.querySelector('#button')
.addEventListener('mouseover', function () {
document.querySelector('#text').style.height = '200px';
});
jsFiddle: https://jsfiddle.net/ew86fb5p/
I hope this solution helps you overcome the challenge!