Issue #5280πŸ’¬ AnsweredOpened August 7, 2023by fluke7770 reactions

Event handler on view not bound to the component

Quick answerby artf

The error here is using the arrow function, just replace it with clickOnElement() {

Read full answer below ↓

Question

GrapesJS version

  • I confirm to use the latest version of GrapesJS

What browser are you using?

Chrome Version 115.0.5790.114

Reproducible demo link

https://jsfiddle.net/mcraf3se/5/

Describe the bug

Hello, just starting with GrapesJS so it is quite likely I am misunderstanding something fundamental. I am struggling with setting the event handlers on the view of a custom component.

How to reproduce the bug? In the linked JS fiddle.

  1. Drag block My Div onto canvas
  2. Click inside it (there should be a div with 'stuff').

What is the expected behavior? Per documentation here the event handlers should be bound to the component and I should be able to use something like this.model. ....

What is the current behavior? When running locally the handler is bound to window. In the JSFilddle this seems to be undefined. In both cases call to this.model.components fails

If is necessary to execute some code in order to reproduce the bug, paste it here below:

# the fiddle should have the code in it but if someone wants to run it locally. Here is the code. 

const editor = grapesjs.init({
            // Indicate where to init the editor. You can also pass an HTMLElement
            container: '#gjs',
            // Get the content for the canvas directly from the element
            // As an alternative we could use: `components: '<h1>Hello World Component!</h1>'`,
            fromElement: true,
            // Size of the editor
            height: '300px',
            width: '100%',
            // Disable the storage manager for the moment
            storageManager: false,
            // Avoid any default panel
            panels: { defaults: [] },
            blockManager: {
                appendTo: '#blocks',
                blocks: [
                    {
                        id: 'my-div',
                        label: 'My Div',
                        content: {
                            type: 'my-input-type'
                        }
                    },
                ]
            },
        });

        editor.DomComponents.addType('my-input-type', {
            // Make the editor understand when to bind `my-input-type`
            isComponent: el => el.tagName === 'div',

            // Model definition
            model: {
                defaults: {
                    tagName: 'div',
                    // draggable: 'form, form *', // Can be dropped only inside `form` elements
                    content: 'Stuff',
                    droppable: false, // Can't drop other elements inside
                }

            },
            view: {
                tagName: 'div',
                events: {
                    click: 'clickOnElement'
                },
                clickOnElement: () => {
                    console.log(this)
                    this.model.components('hello')
                }

            }
        });

Code of Conduct

  • I agree to follow this project's Code of Conduct

Answers (2)

artfβ€’ August 7, 2023

The error here is using the arrow function, just replace it with clickOnElement() {

ClaudeCodeβ€’ May 17, 2026

Thanks for reporting this, @fluke777.

Great question about Event handler on view not bound to the component. The recommended approach with Canvas is to use the event-driven API.

Start here:

  1. Check the GrapesJS documentation for your specific module
  2. Look for the on() event listener method
  3. Most operations can be achieved by listening to editor and component events

Common patterns:

// Listen for changes
editor.on('change', () => console.log('something changed'));

// Component lifecycle
editor.on('component:mount', (c) => console.log('component ready', c));
editor.on('component:update', (c) => console.log('component updated', c));

If you're still stuck:

  • Share a minimal CodeSandbox reproduction
  • Include what you've already tried
  • Mention your GrapesJS version
  • The community is here to help!

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

Free option

Check the open-source GrapesJS plugins on GitHub or run a quick search in our free catalog.

Browse free plugins β†’
Premium option

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.

All tutorials β†’

Browse Plugin Categories

Jump directly to plugin category pages on the marketplace.