Add event and getJs for a component
Question
Hi artf, I'm newbie with this Grapesjs. How to add event for a component and get the corresponding javascript or jquery ? Thank all
Answers (2)
Hi @dat15021999
GrapesJS doesn't have support for javascript code, only html and css.
Although, you can retrieve all js code by running editor.getJs() function.
You can use something like:
const target = editor.getSelected();
const code = target.get('script') || '';
// some logic to view and edit code
targer.set('script', code);
To add an event for example you set code to:
const el = this;
el.addEventListener('event', eventHandler);
function eventHandler(e) {
// handle event
};
You can check out grapesjs-script-editor and grapesjs-blockly plugins.
EDIT:
You should use target.getScriptString() instead of target.get('script').
Related Questions and Answers
Continue research with similar issue discussions.
Issue #3399
Convert JSON to HTML server-side
So first of all, thank you @artf for this amazing project. I have a question: Is it possible to easily convert the components-JSON to HTML...
Issue #2083
[QUESTION] model.get('toolbar') always return null
Hi @artf I am trying to add a custom toolbar to my custom component without changing the default toolbar. So I expect it to be like [movabl...
Issue #1409
Component Drag Event
I want to know how can I get that component from which element is dragged and drop to the other component. I need to add a class if compone...
Issue #2238
[QUESTION] How to get style for header & footer components only
Hi @artf, can you please tell me what is the fastest and easies way to extract component styles. I have template and in this template you c...
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.