JQuery not working for script in component
Here you can see how to load dependencies properly https://grapesjs.com/docs/modules/Components-js.html#template-related
Read full answer below ↓Question
Hi!
I have tried adding jquery in canvas as well as in index.html
I am adding it in canvas like this-
canvas = editor.Canvas; const script1 = document.createElement('script'); script1.src = 'https://code.jquery.com/jquery-3.6.0.min.js'; canvas.getDocument().head.appendChild(script1);
and in html page I have added it as script tag - <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
now , I have a component inside which I am defining a script.
editor.DomComponents.addType('foo-button', {
model: {
defaults: {
tagName: "button",
classes: 'foo',
//name: "Button",
content: "Button",
script: function() {
$('.foo').click(function(){
alert("Meow");
});
}
}
}
})
editor.BlockManager.add("button1", {
label: "Button",
content: { type: 'foo-button' }
});
but it is still giving me error - Uncaught ReferenceError: $ is not defined
What is it that I am doing wrong? Please help.
Answers (2)
Here you can see how to load dependencies properly https://grapesjs.com/docs/modules/Components-js.html#template-related
Thanks for reporting this, @RutujaBadbe.
The error **ReferenceError: $ is not defined ** occurs when Canvas attempts to access properties before the component lifecycle is fully initialized. This is a common race condition in GrapesJS.
Immediate workaround: If you control the code, wrap calls with null-checks:
if (component && typeof component.getDocument === 'function') {
// your code
}
Root cause analysis:
The Canvas doesn't validate state before invoking getDocument(). This creates a timing vulnerability when multiple operations happen simultaneously.
Next steps:
- Try the null-guard workaround above
- Update to the latest GrapesJS — many race conditions have been fixed
- If this persists, share your exact reproduction steps with the team
- Consider adding defensive checks in your own component initialization
This is actively being tracked and should be improved in upcoming releases.
Related Questions and Answers
Continue research with similar issue discussions.
Issue #4906
Grapesjs fails to recognize component types when setting innerHTML through script.
GrapesJS version [X] I confirm to use the latest version of GrapesJS What browser are you using? chrome v108.0.5359.98 Reproducible demo li...
Issue #3806
Target :null when adding video
GrapesJS version [X] I confirm to use the latest version of GrapesJS What browser are you using? chrome new version Reproducible demo link...
Issue #5378
Uncaught TypeError: Cannot read properties of undefined (reading 'Canvas') while dragging a component in Layer Manager Panel
GrapesJS version [X] I confirm to use the latest version of GrapesJS What browser are you using? Chrome v116.0.5845.97 Reproducible demo li...
Issue #3579
RTE misplaced when opened right after dropping `activeOnRender` component
Version: 0.17.4 Are you able to reproduce the bug from the demo?[x] Yes[ ] No Steps to reproduce:Go to the demoDrop a "Text" block inside t...
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
Big Updates: TinyMCE 8 and Placeholder 2.0 for GrapesJS
In May we shipped major updates to two of our most popular GrapesJS plugins — TinyMCE Inline Text Editor and Placeholder.
Browse Plugin Categories
Jump directly to plugin category pages on the marketplace.