grapesjs icon indicating copy to clipboard operation
grapesjs copied to clipboard

[QUESTION] Change and get value of editor.getSelected() block

Open JulyanoF opened this issue 8 years ago • 7 comments

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

JulyanoF avatar Jan 29 '18 20:01 JulyanoF

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

JulyanoF avatar Jan 30 '18 11:01 JulyanoF

@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>')

artf avatar Jan 30 '18 21:01 artf

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?

JulyanoF avatar Jan 31 '18 12:01 JulyanoF

@JulyanoF hi ,bro.i have encounter this problem.can you tell me how to solve it?

ionic666 avatar May 29 '18 05:05 ionic666

@ionic666 Hello. I did something like it: editor.getSelected().toHTML().replace(/<div class="c.*?>([\s\S]*?)<\/div>/, '$1') to get content of selected area and editor.set('content', myContent) to set content

JulyanoF avatar May 29 '18 11:05 JulyanoF

@JulyanoF i am sorry.Excuse me,I have done as you said,But it doesn't work. qq20180530-094232

ionic666 avatar May 30 '18 01:05 ionic666

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

lock[bot] avatar Sep 17 '19 15:09 lock[bot]