Add event and getJs for a component
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.
Read full answer below ↓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...
Check the open-source GrapesJS plugins on GitHub or run a quick search in our free catalog.
Browse free plugins →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.
Tutorial
How to Build a Production GrapesJS Editor: The Complete Walkthrough of Brief, Preset, Plugins, and Services
A complete walkthrough of building a production GrapesJS editor: how to choose a preset, pick plugins, and scope setup services without burning a sprint.
Tutorial
Embed GrapesJS in Your SaaS: A Weekend Guide
Embed GrapesJS in your SaaS and ship a white-label page builder over a weekend. Honest tradeoffs, real code, and the plugins that close the UX gap.
Tutorial
GrapesJS vs Webflow vs Builder.io vs Puck: Which Visual Builder to Choose in 2026
A practitioner's 2026 comparison of GrapesJS, Webflow, Builder.io, and Puck — pricing, AI features, lock-in, and a one-line rule for picking the right one
Browse Plugin Categories
Jump directly to plugin category pages on the marketplace.