Issue #2956Opened August 13, 2020by dat150219992 reactions

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)

RJCAMAugust 17, 20201 reactions

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.

Ju99ernautAugust 17, 20201 reactions

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.

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...

Browse Plugin Categories

Jump directly to plugin category pages on the marketplace.