Issue #2401✓ SolvedOpened November 12, 2019by Indhumathi20166 reactions

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

快速解答by pouyamiralayi4

Hi @Indhumathi2016 you can use find method: cheers!

Read full answer below ↓

Question

代码片段TEXT
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

Answers (2)

👍 Most helpfulpouyamiralayiNovember 12, 2019

Hi @Indhumathi2016 you can use find method:

const wrapper = editor.getWrapper()
const el = wrapper.find('#gist-gdpr')[0]

cheers!

Indhumathi2016November 13, 2019
代码片段TEXT
@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

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.