Issue #820💬 AnsweredOpened January 29, 2018by JulyanoF0 reactions

Change and get value of editor.getSelected() block

快速解答by JulyanoF

To get content: editor.getSelected().get('content') To set content: editor.getSelected().set('content', 'myhtml')

Read full answer below ↓

Question

How can I change and get the content of current selected block?

Answers (3)

JulyanoFJanuary 30, 2018

To get content: editor.getSelected().get('content') To set content: editor.getSelected().set('content', 'myhtml')

artfJanuary 30, 2018

@JulyanoF be aware that content is for static strings (used mainly for custom RTEs) therefore by doing this editor.getSelected().set('content', '<div>TEXT1</div>...') you wouldn't be able to edit inner elements.

const selected = editor.getSelected();

// Get the JSON of the Component
JSON.stringify(selected.toJSON())

// Get its HTML
selected.toHTML();

// Set new components
selected.components('<div>Text1</div><div>Text2</div>')
JulyanoFJanuary 31, 2018

selected.components()

@artf I don't want to create a new component into the selected block,. I need only edit the content. When I use this method, it is creating a new div into the previous block. How can I avoid it?

Related Questions and Answers

Continue research with similar issue discussions.

Paid Plugins That Match This Issue

Curated by issue keywords and label relevance to help you ship faster.

View all plugins

Loading paid plugin recommendations...

Free option

Check the open-source GrapesJS plugins on GitHub or run a quick search in our free catalog.

Browse free plugins →
Premium option

Premium plugins ship with support, regular updates, and production-ready features — save days of integration work.

Browse premium plugins →

Related tutorials

In-depth guides on the same topic.

All tutorials →

Browse Plugin Categories

Jump directly to plugin category pages on the marketplace.