JavaScript Custom Text Editor Buttons Not working

scrummy

I would like to add this text editor to my website but when I click on the formatting buttons it doesnt work.

I have noticed if I add a plus line to the onclick attribute: $('#editor1').focus(); then the button will work, but when it focuses in the textarea, the caret will be placed to the start of the line not to the end which is frustrating. Any idea why this bug appears?

EDIT: Somewhy the undo and redo buttons work withoud the extra $(...).focus() function

// If you enable this, the button will work, but put the text to the start of the line which should be at the end of the line....

/*
$('#setbold').click(() => {
  $('#editor1').focus(); document.execCommand('bold');
});
*/
.flex { display: flex; }
.flex-row { flex-direction: row; } 
.flex-col { flex-direction: column; }
.gap-2 { gap: 2rem; }  .gap-1 { gap: 1rem; } .gap-05 { gap: .5rem; }
.w-50 { width: 50%; } 
[contentEditable=true]:empty:not(:focus):before{ content:attr(data-text) }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="flex flex-col w-50">
   <div class="flex flex-col border-soft" style="border: 1px solid #E4E6EF;">
    <div id="te__con" class="flex flex-row flex-align-c gap-2 padding-05" style="border-bottom: 1px solid #E4E6EF;">
        <div class="flex flex-row flex-align-c gap-05">
            <span class="te__ftstyle italic" onclick="document.execCommand('italic');">
                <span>I</span>
            </span>
            <span class="te__ftstyle bold" id="setbold" onclick="document.execCommand('bold');" >
                <span>B</span>
            </span>
            <span class="te__ftstyle underline" onclick="document.execCommand('underline');">
                <span>U</span>
            </span>
            <span class="te__ftstyle strikethrough" onclick="document.execCommand( 'strikethrough',false,null);" >
                <span>S</span>
            </span>
        </div>
        <div class="flex flex-row flex-align-c gap-05">
            <span class="te__ftstyle redo-apply" onclick="document.execCommand( 'redo',false,null);">
               <span>Redo</span>
            </span>
            <span class="te__ftstyle undo-apply" onclick="document.execCommand( 'undo',false,null);">
                <span>Undo</span>
            </span>
        </div>
        <div class="flex flex-row flex-align-c gap-05">
            <span class="te__ftstyle orderedlist" onclick="document.execCommand('insertOrderedList', false, null);">
                <span>OL</span>
            </span>
            <span class="te__ftstyle unorderedlist" onclick="document.execCommand('insertUnorderedList',false, null)">
                <span>UL</span>
            </span>
        </div>
    </div>
    <div id="editor1" class="small te__editor padding-1 outline-none text-secondary" style="height: 8rem; max-height: 8rem; overflow-y: scroll" contenteditable="true" data-text="Write here...."></div>
</div> 
        </div>

tpliakas

That's how i would do it if I would use the deprecated execCommand

$('button[data-func]').click(function(){
    document.execCommand( $(this).data('func'), false);
});
.editor {
  border: 1px solid grey;
  height: 8rem;
  max-height: 8rem;
  overflow-y: scroll;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<button type="button" data-func="bold">B</button>
<button type="button" data-func="italic">I</button>
<button type="button" data-func="underline">U</button>
<div class="editor" contenteditable></div>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Quill text editor is not working

JavaScript pressing a button to show text not working with multiple buttons

Buttons not working at all (Javascript)

Custom variant text color in buttons

Buttons in slider not working using javascript

JavaScript - Image slider buttons not working

Javascript - radio buttons onlick not working

Creating custom pagination, but next and prev buttons are not working

Changing search text with Javascript Buttons

Update wordpress and my custom buttons on tinymce editor are disappeared

CSS styles in Android Rich Text Editor not working

Regex working in text editor(sublime) but not in python

Knockout validation not working with Kendo text editor

short code not working in wordpress text editor

How to update extracted text in a custom editor view

Android Dev: Custom Radio Buttons with Text?

Text in Custom buttons randomly not centered vertically

Custom Function not working on Stripping Text

Indentation for javascript based custom code editor

Add Javascript into Custom Language - ACE Editor

Quill JavaScript Rich Text Editor restrict tags

javascript text editor - how to capture selection

Text Editor JavaScript issue with additional comma's

How to change the buttons text using javascript

Javascript function that toggles text of different buttons

Enable and disable text with radio buttons with javascript?

How to I replace these two buttons with a text in Javascript

Unity Custom Editor Raycast Not Working Right After When Instantiate Objects

Kendo grid - Grouping is not working with custom editor drop down list

TOP Ranking

HotTag

Archive