Issue #2515✓ SolvedOpened January 19, 2020by ikenderham9 reactions

Allow only one h1 tag on every page

快速解答by artf4

@pouyamiralayi thanks as always for the huge support ❤️ BTW @ikenderham keep in mind that is correct but only if need to check components as first children of editor.getComponents() (which returns children of the main wrapper) If you need to check ALL components (including also nested children) you can use this

Read full answer below ↓

Question

Hello

Is it possible to only allowing one h1 tag on the every page?

I need to make sure that we only can drag one h1 tag into the canvas. What to do? :-)

Answers (3)

👍 Most helpfulartfJanuary 21, 2020

@pouyamiralayi thanks as always for the huge support ❤️

BTW @ikenderham keep in mind that

editor.getComponents().filter(comp => comp.get('tagName') == tag).length > 1

is correct but only if need to check components as first children of editor.getComponents() (which returns children of the main wrapper)

If you need to check ALL components (including also nested children) you can use this

editor.getWrapper().find('h1').length > 1
pouyamiralayiJanuary 20, 2020

@ikenderham

editor.on('component:add', component => {
            const tag = component.get('tagName')
            if (tag == 'h1') {
                if (editor.getComponents().filter(comp => comp.get('tagName') == tag).length > 1) {
                    component.remove()
                }
            }
        })

cheers!

ikenderhamJanuary 20, 2020

You are so nice! How can I thank you?? Thanks alot! Khdafesممنون I will try this example tomorrow on work. I just follwed you on github !

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.