Skip to content

Commit

Permalink
Try a hopefully more reliable first page load approach
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Oct 1, 2023
1 parent a1ecf76 commit 308afa6
Showing 1 changed file with 7 additions and 13 deletions.
20 changes: 7 additions & 13 deletions templates/pages/tools/resizing-textarea.html
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,17 @@ <h1>Single line auto-growing textarea</h1>
<p>I also added this JavaScript to ensure the textarea resizes to the correct size when the page first loads:</p>

<pre>
document.querySelectorAll('textarea').forEach(el => {
el.dispatchEvent(new Event('input', {
bubbles: true,
cancelable: true
}));
document.querySelectorAll('.grow-wrap').forEach(wrap => {
wrap.setAttribute('data-replicated-value', wrap.querySelector('textarea').value);
});
</pre>

<script>
// Set attributes on page load
document.querySelectorAll('.grow-wrap').forEach(wrap => {
wrap.setAttribute('data-replicated-value', wrap.querySelector('textarea').value);
});

// Just for the demo: stash textarea value in localStorage
const KEY = 'resizeTextareaContent';
const ta = document.querySelector('textarea');
Expand All @@ -91,14 +93,6 @@ <h1>Single line auto-growing textarea</h1>
ta.value = saved;
}
ta.addEventListener('input', saveTextarea);

// Trigger input on every textarea on the page, to trigger resize:
document.querySelectorAll('textarea').forEach(el => {
el.dispatchEvent(new Event('input', {
bubbles: true,
cancelable: true
}));
});
</script>
</body>
</html>

0 comments on commit 308afa6

Please sign in to comment.