Issue #2202✓ SolvedOpened August 19, 2019by Chaitwo3 reactions

Remove an Image when on preview and show on editor

Quick answerby artf3

You can't just do $('.img[src=""]') GrapesJS components are not in the same context, are placed inside an iframe so you have to use the document from that iframe, eg.

Read full answer below ↓

Question

Hey, @artf I need help with a challenge I've been facing

I want to hide images with src empty for particular class images in preview mode and show them on editor mode.

I've tried this but it doesn't seem to work for some reason

editor.on('run:preview', () => {
    $('.img[src=""]').hide();
});

editor.on('stop:preview', () => {
    $('.img[src=""]').show();
});

Thanks, Chaitu

Answers (2)

👍 Most helpfulartfAugust 25, 2019

You can't just do $('.img[src=""]') GrapesJS components are not in the same context, are placed inside an iframe so you have to use the document from that iframe, eg.

editor.Canvas.getDocument().querySelectorAll('.img[src=""]')
pouyamiralayiAugust 19, 2019

is your selector correct? As far as i know grapes will set background-image:url() instead of src.

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.