I am trying to make a simple test application for input using HTML, CSS and JS. I want to display some placeholder text in the field that the user enters, so that they know exactly what to enter when they type it. If they deteriorate, this will become obvious, because the placeholder text and input text do not match. I spent more than a few hours trying to figure this out and came up with two sub-solutions: HTML:
<div id='test' class="placeholder" data-value="This appears in the box!"> <textarea id="typed" name="typed" cols=50 rows=15></div>
CSS
.placeholder { position: relative; } .placeholder::after { position: absolute; left: 2px; top: 2px; content: attr(data-value); pointer-events: none; opacity: 0.6; text-align: left; }
This works pretty well, but unfortunately any newlines are completely ignored, displayed as a space, or displayed literally. I tried \ u000A, \ x0A, \ r \ n, just by pressing enter, it does not work.
The second option uses the placeholder attribute:
<textarea placeholder="line1 line2" id="typed" name="typed" cols=50 rows=15></textarea>
This displays correctly, but as soon as you start typing, it disappears.
Does anyone know how to make the standard placeholder text stay for a long time, or have some other workaround that is formatted correctly?
tpeek source share