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 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
Embed GrapesJS in Your SaaS: A Weekend Guide
Embed GrapesJS in your SaaS and ship a white-label page builder over a weekend. Honest tradeoffs, real code, and the plugins that close the UX gap.
Tutorial
GrapesJS vs Webflow vs Builder.io vs Puck: Which Visual Builder to Choose in 2026
A practitioner's 2026 comparison of GrapesJS, Webflow, Builder.io, and Puck — pricing, AI features, lock-in, and a one-line rule for picking the right one
Browse Plugin Categories
Jump directly to plugin category pages on the marketplace.