Change HTML content while block is render in editor
Question
Hello @artf I have one block into block manager like
bm.add("header-with-searchbar",
{
label: '<div class="gjs-block-label">Header with searchbar</div>',
tab: "1",
category: "Portfolio",
content: {
script: function () {
// I am getting id of new div is added by this block at here.
const idOfNewDiv = $(this).attr("id");
// Using below line i am able to edit the DOM, but it will not reflects into editor.getHtml()
document.getElementById(idOfNewDiv).innerHTML = "<p>Hello</p>";
},
content: '<div class="testing"></div>'
}
}
);
As per above script inside the block, i want to add dynamic HTML content while this block is rendered into the UI.
I know that i don't need to change DOM. But need to change using model, but i don't know how can i change it using mode by id/class.
Can you please help me how can i achieve this?
Answers (3)
No @AkibDeraiya123 if you're trying to access the model from the script it means you've totally misunderstood its concept, please read carefully here https://grapesjs.com/docs/modules/Components-js.html#basic-scripts script is for the final HTML (where model, editor and GrapesJS don't exist) not the editor
Thank you for the response @artf , So, it means i can't load dynamic content into blocks while it will render into the editor? If yes then how can i?
Can i replace whole block's html using this.innerHTML = 'New Html content'?
Of course you can, but not via script property. You have to create a custom component and update there its model
Related Questions and Answers
Continue research with similar issue discussions.
Issue #1733
How to remove script tag which is added by add block manager script
Hello @artf I want to export final/production based html content excluding the script(Only which is added by block manager). Suppose i am a...
Issue #1584
Not getting the Script of Block from editor.getComponents()
Below is the Component that I'm adding. this.editor.BlockManager.add('dob-mmyy-pii', { id: 'dob-mmyy', label: DOB (MM, YYYY) (PII), categor...
Issue #511
Cannot edit the content after update it via block manager API
Hello @artf sorry for my last issue, I must create a new issue, my problem is got error like this EDIT : sorry i just fixed my last issue w...
Issue #555
Add external script in script while creating new component
Hello @artf How can I pass external script link while creating a component? like this way, bm.add('test-block2', { label: 'Test block2', at...
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.