0

It is possible to create a ckeditor using for loop statement in javascript? Everytime I generate a ckeditor within textarea, the ckeditor is not show up. Any idea. Thank you.

<script type="text/javascript">
 for(var index = 1; index <= 4; index++ ){
  str += '<textarea class="ckeditor" name="answer[]"></textarea>';
 }
</script>

Sample Image: enter image description here

1
  • bro, were you able to solve this issue? I've been stuck with it for a couple of hours already :( Commented Jan 23, 2021 at 17:19

1 Answer 1

1

Looking at the ckeditor5 readme, it appears that you need to have the HTML textarea(s) already inserted and then call the create method for each textarea, passing its id as the parameter. I pulled this from the readme:

In your HTML page add an element that CKEditor should replace:

<textarea name="content" id="editor"></textarea>

Load the classic editor build (you can choose between CDN, npm and zip downloads):

<script src="https://cdn.ckeditor.com/ckeditor5/<version>/classic/ckeditor.js"></script>

Call the ClassicEditor.create() method:

<script> 
   ClassicEditor .create(
      document.querySelector( '#editor' )
   ) .catch( error => { console.error( error ); } );
</script>

You’re ready to go!

So, following those instructions, you should be able to call create for each textarea, in your loop, as long as it's been added to the document before you call create().

Edit: If you really want to use an older version of ckeditor, it looks like a previous question mentions using .replace() or .replaceClass()

Sign up to request clarification or add additional context in comments.

2 Comments

I tried your answer but this is not working. ID on textarea are not good idea because you generate a textarea using for loop with the unique ID so that I tried to use a class instead of ID on textarea but doesn't work. By the way thank you for responding
@YadYoung The selected answer to the following question recommends using appendChild so that your new elements are added to the DOM, and a few other good suggestions to make sure that you can access your dynamically added elements: stackoverflow.com/questions/9902803/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.