Removing HTML element in CKEditor

10,492

Solution 1

Content of the CKEditor is kept in the element which you can access by editor.editable(). Then, you can use methods like dom.element.find() or dom.element.findOne() and finally you can remove element using dom.element.remove(). You can also access native DOM node and use jQuery.

Example using CKEditor API:

editor.editable().findOne( 'img' ).remove();

Using jQuery:

jQuery( editor.editable().$ ).find( 'img' ).remove();

Solution 2

You can do it like this

CKEDITOR.instances.editor_obj.document.getById(id).remove();

or

editor_obj.document.getById(id).remove();
Share:
10,492
Matthew Groves
Author by

Matthew Groves

Something about the curly brace speaks to me. Matthew D. Groves is a guy who loves to code. It doesn't matter if it's C#, jQuery, or PHP: he'll submit pull requests for anything. He has been coding professionally ever since he wrote a QuickBASIC point-of-sale app for his parent's pizza shop back in the 90s. He currently works as a Product Marketing Manager for Couchbase. His free time is spent with his family, watching the Reds, and getting involved in the developer community. He is the author of AOP in .NET (published by Manning), a Pluralsight author, and a Microsoft MVP.

Updated on June 04, 2022

Comments

  • Matthew Groves
    Matthew Groves almost 2 years

    I am creating a CKEditor plugin. As part of this plugin, I would like to be able to remove some arbitrary HTML element from the editor's content. An <img id="remove-me" /> for instance.

    I know I can get the contents (var contents = e.getData();) and replace the contents with something else (e.setData(newContents);). I know I could do a string/regex replace, but that gets tricky since the user may add some arbitrary attributes or spacing to the HTML.

    I would love to be able to use something like jQuery to find and remove the element (like $("#remove-me").remove(), but don't know of a way to do this.

    Any suggestions?

  • Matthew Groves
    Matthew Groves about 10 years
    Is this CKEditor 4 only, or is it also available in CKEditor 3?
  • Reinmar
    Reinmar about 10 years
    CKEditor 4 only. In CKEditor 3 you can use jQuery( editor.document.$ ).
  • ValRob
    ValRob over 4 years
    After do that, I get Uncaught TypeError: Cannot read property 'attributes' of null when I try to change to html view. Any Idea why?