I'm developing a function on my website where a user should be able to edit his or hers own topic using ckeditor 5 and a textarea. The textarea is placed inside a modal. However, when I try to prefill the textarea when a user pushes a button, nothing goes inside the textarea. I have tried the following:
var editor;
ClassicEditor
.create(document.querySelector('#edit-reply-modal'))
.then(editor => {
editor = editor;
})
$(".toggle-edit-modal").click(function(e) {
e.preventDefault();
editor.setData("<p>Testing</p>"));
$("#edit-reply-modal").html("<p>Testing</p>");
});
Any help is appreciated.
value=data; $('#textareaID'). val(data);
In a nutshell: CKEditor 4 leverages the browser as much as possible. Consequence: any tiny difference in behavior across browsers in low-level APIs has a huge negative impact. CKEditor 5 therefore avoids using the browser for many low-level operations, and re-implements many things.
CKEditor docs says the replace method takes an element argument : element : Object/String The DOM element (textarea), its ID, or name. So, you can use document. getElementById to select your textarea and then use CKEditor.
I see you have one ')' more than needed editor.data.set("<p>Testing</p>"));
If you still can't set data, than try to set data like this :
editor.data.set("<p>Testing</p>");
HTML Code:
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.ckeditor.com/ckeditor5/24.0.0/classic/ckeditor.js"></script>
<textarea id="edit-reply-modal"><p>Old Data</p></textarea>
<button id="toggle-edit-modal">Fill New Data</button>
JAVASCRIPT Code:
let YourEditor;
ClassicEditor
.create(document.querySelector('#edit-reply-modal'))
.then(editor => {
window.editor = editor;
YourEditor = editor;
})
$('#toggle-edit-modal').on('click', function() {
YourEditor.setData('<p>This is the new Data!</p>');
})
let YourEditor;
ClassicEditor
.create(document.querySelector('#edit-reply-modal'))
.then(editor => {
window.editor = editor;
YourEditor = editor;
})
$('#toggle-edit-modal').on('click', function() {
YourEditor.setData('<p>This is the new Data!</p>');
})
button{
margin-top:20px;
padding: 5px;
color: white;
background: #00F;
border-radius: 5px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.ckeditor.com/ckeditor5/24.0.0/classic/ckeditor.js"></script>
<textarea id="edit-reply-modal"><p>Old Data</p></textarea>
<button id="toggle-edit-modal">Fill New Data</button>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With