How to change id/class when new element drop on editor or render element with dynamic ip?
Question
Hello @artf , Hope you are doing well.
I have two questions regards dynamic blocks adding,
- Is it possible to render the block with dynamic id?
- Is it possible to change the id/class/onClick methods in
block:drag:stoporcomponent:addgrapesjs 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)
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}')`,
})
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?
@AkibDeraiya123 you can do whatever you want, check the API please https://grapesjs.com/docs/api/component.html
Related Questions and Answers
Continue research with similar issue discussions.
Issue #1951
[QUESTIONS] change or add event on add component from block
is it possible ?, change the drag and drop event to on click when adding a new component, I want to add components by clicking the block th...
Issue #1817
Change HTML content while block is render in editor
Hello @artf I have one block into block manager like As per above script inside the block, i want to add dynamic HTML content while this bl...
Issue #1674
Render block after trait action .
I have created new block called sidebar that have dynamic updates by trait component ,trait contain checkboxes to dynamic change content by...
Issue #1111
How to add parent class when drag element
Hi @artf, Hope you are doing fine. Please let us know how we can add the parent class of div when new element is dragged inside that div li...
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.