Add new class programatically
Question
Hello Artf, I want to edit the styles of components (mostly imported html) without modifying all components with the same class. I tried to add a random named class programmatically like this: this.editor.on('component:selected', function(model) { if (model && model.length > 2) { const uuid = this.s4(); // generate new random class name const selector = { name: uuid, type: 'class', label: uuid, }; model[1].attributes.classes.push(selectorManager.get(uuid)); let mClasees = model[1].attributes.classes; const uuid = this.s4(); const selector = { name: uuid, type: 'class', label: uuid, }; mClasee...
Answers (3)
@wemod123 editor.getWrapper().addClass('my-class')
@IrinaCodixis from the https://github.com/artf/grapesjs/releases/tag/v0.14.52 we've added setRule/getRule methods, so to add some CSS rule programatically you should do this:
You mean something like model.set('style', {...})? This adds an inline style on the component
Related Questions and Answers
Continue research with similar issue discussions.
Issue #3211
How to move components
Hello dear: I want move a component to first/end in the wrapper programmatically, i think something like editor.getWrapper().add(editor.sel...
Issue #1817
Change HTML content while block is render in editor
Hello @artf I have one block into block manager like As per above script inside the block, i want to add dynamic HTML content while this bl...
Issue #791
Adding new button in toolbar
Hello, I am trying to add a new button to the toolbar of all the components. I been able to add for each component separatly, but not for a...
Issue #3375
Cannot add spaces to extended button component based on text component.
Hello guys, First of all i would like to express my gratitudes for this amazing tool. I have extended button component based on text compon...
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
Vanilla GrapesJS vs GrapesJS With Plugins: What You Actually Get Out of the Box (2026)
New to GrapesJS? See exactly what the stock editor gives you, where it falls short, and which plugins close the gaps — a beginner-friendly 2026 comparison
Tutorial
Lighthouse for GrapesJS: Audit Accessibility and SEO Without Leaving the Editor
Live WCAG 2.1/2.2 accessibility auditor + SEO manager for GrapesJS. Every finding is bound to the exact component — click to select, then one-click fix.
Tutorial
GrapesJS DevTools: A Developer Panel Inside Your Editor
If you've ever debugged GrapesJS with console.log(editor.getSelected()) and a dozen throwaway event listeners — this post is for you.
Browse Plugin Categories
Jump directly to plugin category pages on the marketplace.