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
How to Build an AI-Powered Page Builder with GrapesJS in 2026
What we learned building GJSDocs — from simple AI text generation to structured, context-aware visual editing
Tutorial
How Much Does It Cost to Build a SaaS Page Builder in 2026?
How much does it cost to build a SaaS page builder in 2026? Explore realistic development costs, timelines, features, and how using GrapesJS can reduce
Tutorial
PageKit: the GrapesJS application you'd have to build anyway
A source-available Next.js app around the open-source GrapesJS core — publishing, custom domains, leads, checkout and a plugin loader.
Browse Plugin Categories
Jump directly to plugin category pages on the marketplace.