How to Achieve Float Label Placeholder in JavaScript Rich Text Editor?
The JavaScript Rich Text Editor does not have built-in functionality for an outlined float label placeholder. However, you can achieve this effect by utilizing focus and blur events in your application. Below is a code snippet that demonstrates how to implement this feature.
Implementing the Rich Text Editor
Initialize the Rich Text Editor control by configuring a simple set of tools based on your requirements. And, define a placeholder
variable, which will act as an outlined floating label to provide contextual guidance to users. Then add the focus, blur and toolbarClick events to the Rich Text Editor.
let defaultRTE = new RichTextEditor({
placeholder: 'Rich Text Editor',
toolbarSettings: {
items: [
'Bold',
'Italic',
'Underline',
'Formats',
'-',
'Alignments',
'OrderedList',
'UnorderedList',
'CreateLink',
],
},
focus: focus,
blur: blur,
toolbarClick: toolbarClick,
});
defaultRTE.appendTo('#defaultRTE');
Handling Focus and Blur Events
-
The
focus
function adds a class to the placeholder and the content area when the editor is focused, while theblur
function removes these classes if the editor is empty. -
The
keydown
event listener is added to the Rich Text Editor’s input element to ensures that the placeholder remains visible when typing in the editor.
Here is a sample code snippet demonstrating how to achieve this:
var placeholder = document.getElementsByClassName('e-rte-placeholder')[0];
var rteContent = document.getElementsByClassName('e-richtexteditor')[0];
function blur() {
placeholder.style.display = 'block';
placeholder.style.top = '35px';
if (defaultRTE.value == null) {
placeholder.classList.remove('float');
rteContent.classList.remove('floatContent');
}
}
function focus() {
document
.getElementsByClassName('e-rte-content')[0]
.addEventListener('keydown', function () {
placeholder.style.display = 'block';
});
placeholder.classList.add('float');
rteContent.classList.add('floatContent');
document
.getElementsByClassName('e-richtexteditor')[0]
.appendChild(document.getElementsByClassName('e-rte-placeholder')[0]);
}
function toolbarClick() {
document.getElementsByClassName('e-rte-placeholder')[0].style.display = 'block';
}
Adding styles for CSS classes
The .float
and .floatContent
classes are used to style the placeholder and the editor when focused.
.float {
top: -25px !important;
color: deeppink !important;
opacity: 1 !important;
background: white;
z-index: 3 !important;
height: 20px;
width: auto;
left: 16px;
}
.floatContent {
overflow: visible !important;
border: 2px solid deeppink !important;
}
Conclusion
I hope you enjoyed learning how to achieve float label placeholder in JavaScript Rich Text Editor.
You can refer to our JavaScript Rich Text Editor feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications.
You can also explore our JavaScript Rich Text Editor Example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!