Grapesjs: How to check a component or element is present in canvas or not

Created on 12 Nov 2019  路  2Comments  路  Source: artf/grapesjs

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.

Thanks

Most helpful comment

@pouyamiralayi
It's working fine when i do like this

editor.on('load', () => {
const wrapper = editor.getWrapper()
const el = wrapper.find('#gist-gdpr')[0]
});

Thanks

All 2 comments

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]
});

Thanks

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kickbk picture kickbk  路  3Comments

mathiasbc picture mathiasbc  路  3Comments

faizansaiyed picture faizansaiyed  路  3Comments

krunal039 picture krunal039  路  3Comments

alibouaziz picture alibouaziz  路  3Comments