selected content parent tag remove
Question
https://github.com/artf/grapesjs/issues/906#issuecomment-381403858
I need to remove the html content from editor.getSelected(). Now I'm using,
var selectedContent = editor.getSelected();
selectedContent.set('content', '');
selectedContent.components(MYHTML);
While I use selectedContent.set('content', ''); it remove all the inner html. But didn't remove the parent tag. For example,
Before: <h2 id="it4a"><p>Sample Update Check Done</p></h2>
After: <h2 id="it4a"></h2>
After set the components, its like <h2 id="it4a">NEW HTML</h2>. How to remove the parent tags while set the innerHtml empty?
Guide me how to solve this?
Answers (3)
Have you seen this #1077 answer by @artf I think it's suited with your case.
const component = editor.getSelected();
const coll = component.collection;
const at = coll.indexOf(component);
coll.remove(component);
return coll.add('<div>New content</div>', { at });
Yeah I tried @afdallah, It returns indexOf undefined. Let me try again. Thanks for your response :)
Hi @afdallah @artf I tried the above concept. It works fine in first time. While I try to update second time, it didn't work.

Related Questions and Answers
Continue research with similar issue discussions.
Issue #2853
[QUESTION] Do you have plans to remove dependency on Document.execCommand in RTE?
I noticed that in https://github.com/artf/grapesjs/issues/74 when the move to the Pell based editor is listed it's use of Document.execComm...
Issue #3245
grapesjs-custom-code doesn't execute <script> from GrapesJS 0.16.30 and up
grapesjs-custom-code work fine with script tag until GrapesJS 0.16.27, after this version, no script content is executed. ✔️ Editor init()...
Issue #2616
how to use editor.getSelected().append(`<div>...`);
WARNING READ and FOLLOW next 5 steps, then REMOVE them before posting the issueFollow CONTRIBUTING Guidelines https://github.com/artf/grape...
Issue #1688
on set model content property image and text written in content is not editable in grapejs canvas.
WARNING READ and FOLLOW next 5 steps, then REMOVE them before posting the issueFollow CONTRIBUTING Guidelines https://github.com/artf/grape...
Paid Plugins That Match This Issue
Curated by issue keywords and label relevance to help you ship faster.
Loading paid plugin recommendations...
Browse Plugin Categories
Jump directly to plugin category pages on the marketplace.