Issue #1817Opened February 20, 2019by AkibDeraiya1230 reactions

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)

artfFebruary 22, 20190 reactions

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

AkibDeraiya123February 22, 20190 reactions

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'?

artfFebruary 22, 20190 reactions

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.

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...

Browse Plugin Categories

Jump directly to plugin category pages on the marketplace.