Issue #1683💬 AnsweredOpened December 26, 2018by AkibDeraiya1230 reactions

How to change id/class when new element drop on editor or render element with dynamic ip?

快速解答by artf

In your block:drag:stop callback you should make a change to the droppedComponent (is an instance of Component, so use its API), the $('#staticId') doesn't even exist, the canvas is in the other frame of the document.

Read full answer below ↓

Question

Hello @artf , Hope you are doing well.

I have two questions regards dynamic blocks adding,

  1. Is it possible to render the block with dynamic id?
  2. Is it possible to change the id/class/onClick methods in block:drag:stop or component:add grapesjs methods?

Here is some of my code,

bm.add("image-cropper",
        {
          label: '<div class="gjs-block-label">Image Cropper</div>',
          tab: "2",
          content: '<div id="staticId" onclick="jsFunc(staticId)"><h1>Hello</h1><div>'
        }
      )

In above example i need to make dynamic instead of staticId. Which is also passed into the onclick function as well.

I am trying to change that id inside the

editor.on('block:drag:stop', function (droppedComponent) {
      $('#staticId').attr('id', 'random id is here');
}

But after the change with the help of $('#staticId').attr('id', 'random id is here'); it's not getting change actually.

Actually i am using grapes.js with angular.js(Angular 1).

So, can you please suggest me is there any way to solve my problem using grapes.js? If you think it can be solve using jQuery then please also suggest me that way too.

Answers (3)

artfDecember 27, 2018

In your block:drag:stop callback you should make a change to the droppedComponent (is an instance of Component, so use its API), the $('#staticId') doesn't even exist, the canvas is in the other frame of the document.

const randomId = '...';
droppedComponent.addAttributes({
	id: randomId,
	onclick: `jsFunc('#${randomId}')`,
})
AkibDeraiya123December 27, 2018

Hello @artf, Thank you for your quick support.

It's working for me now,

droppedComponent.set({
    attributes: {
        id: 'randomId',
        onclick: 'jsFunc("randomId")'
    }
})

This is worked for me.

Suppose i have content like,

bm.add("image-cropper",
        {
          label: '<div class="gjs-block-label">Image Cropper</div>',
          tab: "2",
          content: '<div><h1>Hello</h1><img id="staticId" onclick="jsFunc(staticId)" src="imagePath" /><div>'
        }
      )

Is there any possibilities to update img tag's id and onclick function?

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.