Allow only one h1 tag on every page
@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)
@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
@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!
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.
Issue #1804
Applying two different traits on same html tag but applies only one
Hello @artf , If we need to initialize two traits on same HTML tag then it's possible or not? Actually i define two different traits on sam...
Issue #2744
[QUESTION] Disable a block drag&drop
Hello everyone, I would like to make forbidden the drop of one block before another specific one. Context : i've create a new trait for som...
Issue #4273
How to trigger the 'Import Template (modal)' on page load
Hello, Please i'm in need of the logic on how to the import template modal on initialization of the page... I'm using the newsletter preset...
Issue #3482
Runtime updated traits not EDIT mode.
Hello @artf I having issue with a traits. When I drag and drop the block to canvas it's traits showing fine in under settings section. but...
Paid Plugins That Match This Issue
Curated by issue keywords and label relevance to help you ship faster.
Loading paid plugin recommendations...
Check the open-source GrapesJS plugins on GitHub or run a quick search in our free catalog.
Browse free plugins →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.
Tutorial
How to Build a Production GrapesJS Editor: The Complete Walkthrough of Brief, Preset, Plugins, and Services
A complete walkthrough of building a production GrapesJS editor: how to choose a preset, pick plugins, and scope setup services without burning a sprint.
Tutorial
10 Mistakes When Building a Visual Editor (And How to Avoid Them)
Visual editors are powerful tools — they enable non-technical users to create websites, emails, landing pages, and applications without writing code
Tutorial
GrapesJS and AI: The Future of No-Code Web Design
GrapesJS, a popular drag-and-drop web builder framework, is now getting an AI-powered makeover
Browse Plugin Categories
Jump directly to plugin category pages on the marketplace.