issue
I encountered an issue where <textarea>
and Javascript had some quirks when trying to transfer the value into a <pre>
tag. My aim was to implement a blinking caret in the <pre>
as if I were pressing F7. The reason behind not using just the <textarea>
is because I am currently working on a JSON formatter project with added syntax highlighting for entertainment purposes.
current setup
document.querySelector("pre.stuff").addEventListener("click", function() {
document.querySelector("textarea.text").focus();
});
document.querySelector("textarea.text").addEventListener("input", function() {
document.querySelector("pre.stuff").innerText = this.value;
});
textarea.text {
position: absolute;
border: none;
padding: 0px;
opacity: 0;
width: 0px;
height: 0px;
}
pre.stuff {
width: calc(100% - 20px);
height: 250px;
border: solid gray 2px;
margin: 0px;
border-radius: 5px;
padding: 10px;
transition: 0.3s;
font-family: "Source Code Pro", monospace;
tab-size: var(--tab-size);
overflow: scroll;
cursor: text;
}
textarea.text:focus ~ pre.stuff, pre.stuff:focus {
outline: none;
box-shadow: #0066ff55 0px 0px 0px 4px; /* glowwww */
border: solid #66a3ff 2px;
}
body {
margin: 16px;
}
<link href="https://fonts.googleapis.com/css2?family=Source+Code+Pro&display=swap" rel="stylesheet">
<textarea class="text"></textarea>
<pre class="stuff" spellcheck="false" tabindex="-1"></pre>
<br>
I decided to exclude the syntax highlighting aspect :P
In addition, it's crucial for the focus to remain on the <textarea>
for the functionality to properly work.