[Question] Add script using drag and drop that will add elements dynamically
Question
Hey. Thank you for this great project. I'm using it in one of my project and need some help.
1- I have a block element with script in it. This script add a form dynamically inside a iframe.
blockManager.add('custom-script', {
label: 'email-custom-script',
category: 'Forms',
content: `
<div>
<script type="text/javascript" src="https://app.getresponse.com/view_webform_v2.js?u=zqQFS&webforms_id=SGRbz"></script>
<div>`,
attributes: {
title: 'Get Response Form',
class: 'fa fa-square'
}
})
When I drag and drop the script in anywhere on the page. Grapesjs ignore the position and render the form at the end of the page.

But when I export the HTML and CSS from grapesjs the position is correct where it should be. Why this is happening.

2- When I enable "allowScripts: 1," in grapesjs init function. I can add scripts but can't drag and drop (use) my custom block elements
let blockManager = editor.BlockManager;
let snippets = await this.getSectionSnippets();
snippets.map((item) => {
blockManager.add(item.name, {
label: item.name,
category: 'Sections',
content: `<html><head><script></script><style>${item.css}</style></head><body>${item.html}</body></html>`,
attributes: {
title: item.name,
class: 'fa fa-square'
}
})
});```
If a disable "allowScript" then I can use my custom block element but can't add scripts.
Answers (3)
Finally it is solved @pouyamiralayi Thank you for helping me out. After using your solution. It insert the script at correct position.
Hi! did you try putting your script in the script tag? this way it will be executed at the end and probably will respect the order you desire:
editor.BlockManager.add('form-block', {
script: function(){/* your-js */}
}
cheers.
When I drag and drop the script in anywhere on the page. Grapesjs ignore the position and render the form at the end of the page.
To be honest I don't know, it's all depended by the third-party library (in your case GetResponse) and how it appends content once loaded
When I enable "allowScripts: 1," in grapesjs init function. I can add scripts but can't drag and drop (use) my custom block elements
First of all, there is no need to use html, head, body tags in your blocks. You're dropping HTML snippets inside a body of already defined document...
I highly suggest you read about Components as you'll get much more control than simple blocks
Related Questions and Answers
Continue research with similar issue discussions.
Issue #1499
[Question] Is it possible to perform dom manipulation through the block's contents script?
Hey! First of all thank you so much to build such a great and versatile tool. Is it possible that I can manipulate the dom elements of the...
Issue #2961
BUG: Components in custom modal isn't editable
Hello, I'm trying to add custom modal block to my project and I want to change content of this modal also with drag&drop. Here's code of my...
Issue #493
[QUESTION] Can i set content dynamically from block API ?
Hi @artf , very great web builder framework and keep it up :+1: code : As you can see, can i make tag dynamically from my backend? i use EJ...
Issue #3207
Form is not submitting
I am trying to create as custom block. But the form is not submitting when i use components inside content. I am able to get all the form d...
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.