A tag inside button is removed
This happens because of the button implementation in the forms plugin, which allows only textnodes as children. If you remove the form plugin you'll be able to see the <a> element (if you prefer you can also extend the button component with your own logic).
Read full answer below βQuestion
GrapesJS version
- I confirm to use the latest version of GrapesJS
What browser are you using?
Chrome Latest
Reproducible demo link
https://codesandbox.io/s/interesting-bell-xntbtr
Describe the bug
How to reproduce the bug?
- go to the codesandbox
- drag and drop Custom Code block.
- put this code inside the Custom Code editor and click Save:
<button class="btn bg-primary text-white">
<a href="https://www.example.com/" target="_blank">Click me</a>
</button>
- Click the button.
- The button should open the href link when you click it but it's not. Noticed that the a tag is removed inside the button tag
What is the expected behavior?
<a> tag is not removed
Code of Conduct
- I agree to follow this project's Code of Conduct
Answers (4)
This happens because of the button implementation in the forms plugin, which allows only textnodes as children.
If you remove the form plugin you'll be able to see the <a> element (if you prefer you can also extend the button component with your own logic).
so what is the code of "normal" implementation of the button? btw, may I know why do you make the button to behave like that on the forms plugin?
If I recall properly the main reason there was a problem with inline text editing with button elements, so that approach was the easiest way to fix a common issue.
I guess the "normal" implementation would be to skip/overwrite the init method but you should think on how to properly handle the inline text editing.
Thanks for reporting this, @handhikadj.
Great question about a tag inside button is removed. The recommended approach with GrapesJS is to use the event-driven API.
Start here:
- Check the GrapesJS documentation for your specific module
- Look for the
on()event listener method - Most operations can be achieved by listening to editor and component events
Common patterns:
// Listen for changes
editor.on('change', () => console.log('something changed'));
// Component lifecycle
editor.on('component:mount', (c) => console.log('component ready', c));
editor.on('component:update', (c) => console.log('component updated', c));
If you're still stuck:
- Share a minimal CodeSandbox reproduction
- Include what you've already tried
- Mention your GrapesJS version
- The community is here to help!
Related Questions and Answers
Continue research with similar issue discussions.
Issue #4732
Tabs behaviour after HTML import
GrapesJS version [X] I confirm to use the latest version of GrapesJS What browser are you using? Chrome 107 (MacOS) Reproducible demo link...
Issue #6348
It's possible to drag and drop literally anything inside grapesjs canvas
GrapesJS version [X] I confirm to use the latest version of GrapesJS What browser are you using? Chrome Reproducible demo link https://grap...
Issue #5199
Changes in component's attributes being reproduced in all the instances instead of just one
GrapesJS version [X] I confirm to use the latest version of GrapesJS What browser are you using? Chrome v114 Reproducible demo link https:/...
Issue #6365
Style Manager Fails to Interpret Descendant Selectors Correctly
GrapesJS version [X] I confirm to use the latest version of GrapesJS What browser are you using? Chrome Version 131.0.6778.205 Reproducible...
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
Big Updates: TinyMCE 8 and Placeholder 2.0 for GrapesJS
In May we shipped major updates to two of our most popular GrapesJS plugins β TinyMCE Inline Text Editor and Placeholder.
Tutorial
Find the Right GrapesJS Plugin in Seconds: Smarter Discovery Is Live
We're shipping a set of discovery upgrades. New label filters, a proper compatibility switch for GrapesJS vs Studio, one-click and a smarter sort bar.
Browse Plugin Categories
Jump directly to plugin category pages on the marketplace.