How to check a component or element is present in canvas or not
Question
I have been trying to enable a checkbox in trait on load, when a element is present in canvas but unfortunately i don't know how to check the element is present or not, i'm trying the following code in onRender() method
const canvasElement = editor.DomComponents.getWrapper();
let isGdprEnabled = false;
if (canvasElement.getEl().getElementById('gist-gdpr')) {
isGdprEnabled = true;
}
when i tried the above code, i got error "getElementById is not a function" if i used getElementsByClassName it return the HtmlCollection but when i trying to get the length of HtmlCollection it returns 0, but actually there is one element inside the HtmlCollection.
Is there any other way to do it ?, pls help me in this.
ThanksAnswers (2)
Hi @Indhumathi2016 you can use find method:
const wrapper = editor.getWrapper()
const el = wrapper.find('#gist-gdpr')[0]
cheers!
@pouyamiralayi
It's working fine when i do like this
editor.on('load', () => {
const wrapper = editor.getWrapper()
const el = wrapper.find('#gist-gdpr')[0]
});
ThanksRelated Questions and Answers
Continue research with similar issue discussions.
Issue #3704
load blocks closed by default?
I am using the following bit of code to collapse all the block categories, unfortunately, this is not working live demo : https://jsfiddle....
Issue #956
[QUESTION] How to load a new trait I added in a built-in component?
Hi guys I added a trait for my Link element which is a built-in component for gjs-blocks-basic. When I drag a new link component to the can...
Issue #2055
[QUESTION]: How to remove the components correctly?
I want to remove the components programmatically, the following is my code snippet. After the components have been removed, then undo the d...
Issue #3461
Re-render trait on selected component
I know there is a function that can rerender the view of a trait. However, I still don't know how and where to use it.
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.