Answering my own question -- complete with a dataset attribute to check if the 'value' of the textarea was entered by the user or is from the placeholder.
function addMultilinePlaceholder(textarea, placeholder) {
// Does textarea have input entered by the user?
textarea.dataset.hasValue = '';
textarea.value = placeholder;
textarea.addEventListener('focus', function () {
if (!textarea.dataset.hasValue) {
this.value = '';
}
});
textarea.addEventListener('keyup', function () {
textarea.dataset.hasValue = (textarea.value.length > 0) ? 'true' : '';
});
textarea.addEventListener('blur', function () {
if (!textarea.dataset.hasValue) {
this.value = placeholder;
}
});
}
Fiddle: https://jsfiddle.net/fm4vyxjL/1/
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…