STOP PRESS: The original code for underline was buggy - the code below seems to be working as expected. Apologies to anyone who had tried and got frustrated …
For CLASSIC Knack apps…
I’ve seen a few feature requests to add Underline to the rich text toolbar. Here is a way to do it.
// ADD UNDERLINE BUTTON TO REDACTOR TOOLBAR AT POSITION 3
$(document).on('knack-view-render.any', function() {
$('.redactor-toolbar').each(function() {
$(this).find('.re-underline').closest('li').remove();
var btn = '<li><a href="#" class="re-icon re-underline" rel="underline" tabindex="-1"></a></li>';
$(this).children('li').eq(3).after(btn);
});
});
// HANDLE UNDERLINE BUTTON CLICK
$(document).on('mousedown', '.re-underline', function(e) {
e.preventDefault();
});
$(document).on('click', '.re-underline', function(e) {
e.preventDefault();
document.execCommand('underline', false, null);
$('.redactor-editor').focus();
var $btn = $(this);
setTimeout(function() {
$btn.toggleClass('redactor-act', document.queryCommandState('underline'));
}, 10);
});
You can vary which position in the menu it is inserted by changing the .eq(3) to whatever position you would prefer.
Here is also some JS to stop the Rich text editor from creating a new paragraph when user presses enter - it allows the editor to work like a normal text editor.
// INSERT LINE BREAK INSTEAD OF PARAGRAPH ON ENTER IN RICH TEXT FIELDS
$(document).on('keydown', '.redactor-editor', function(e) {
if (e.key === 'Enter' && !e.shiftKey) {
e.preventDefault();
document.execCommand('insertLineBreak');
}
});
Finally, I leave a link to KnackPros incredibly useful post which tells us how to customise what is on that toolbar, so you can remove anything you don’t want your users to do - in my case it was them getting into the HTML view, formatting options, and adding complex content like images, videos and tables, etc. I just wanted a simple editor, one step up from paragraph text…
If you use KnackPros guidance, and you have also added Underline, make sure underline is one of the things you add to the list of exclusions. otherwise it won’t show! Dont’ ask how long it took me to realise that… ![]()