Grapesjs: Remove an Image when on preview and show on editor

Created on 19 Aug 2019  路  2Comments  路  Source: artf/grapesjs

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

Most helpful comment

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=""]')

All 2 comments

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

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=""]')
Was this page helpful?
0 / 5 - 0 ratings

Related issues

gabrigcl picture gabrigcl  路  25Comments

bungambohlah picture bungambohlah  路  14Comments

fzs1994 picture fzs1994  路  15Comments

TrungPham383 picture TrungPham383  路  21Comments

artf picture artf  路  48Comments