Currently, I am attempting to create an editable div with a placeholder. My goal is for the placeholder to be automatically displayed when there is no text in the div.
To achieve this, I am using the contentEditable='true';
attribute on the div element. The placeholder functionality is being handled through a jQuery function as shown below:
$('div').on('activate', function() {
$(this).empty();
var range, sel;
if ((sel = document.selection) && document.body.createTextRange) {
range = document.body.createTextRange();
range.moveToElementText(this);
range.select();
}
});
$('div').focus(function() {
if (this.hasChildNodes() && document.createRange && window.getSelection) {
$(this).empty();
var range, sel;
range = document.createRange();
range.selectNodeContents(this);
sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);
}
});