Two html tags as one (link/button)
Question
Is it possible to have two elements (div and lnik) and consider as a single? For example: there is the my following link block:
<div class="row"><a class="btn" href="http://test.com">Link Here</a></div>
And I would like that on click div or button, it shows the <a> as a selected element on editor views, but in wrapper, show the div as a selected.
Answers (3)
You should create a custom component
@artf I tried extending original link component, but it seems not working
var linkType = editor.DomComponents.getType('link');
domComps.addType('link', {
model: linkType.model.extend({
defaults: Object.assign({}, linkType.model.prototype.defaults, {
traits: [
{
label: 'Link',
name: 'href',
placeholder: 'http://meusite.com.br'
},
{
label: 'Conteúdo',
type: 'input-conteudo',
placeholder: 'Botão'
},
{
label: 'Cor',
type: 'color',
name: 'background-color'
}
],
toHTML: function() {
return '<div class="row"><a class="btn">Link Here</a></div>';
},
}),
})
});
It's not changing anything..
I already tried to disable autorender and render after all changes.
I could change the content only setting a new, using BlockManager:
bm.get('c85').set({ content: '<div class"row"><a class="btn">Link Here</a></div>' });
But when click in button, it's selecting only the button (not div) and when I click div, it's not selecting my button custom settings;
@artf as you said, I tried to use Components, but I didn't get expected result. Could you help me?
My link block prints this content:
<div style="height:45px;margin:0 auto 10px auto;padding: 5px 5px 5px 5px;width: 100%;text-align:center"><a class="btn btn-purple">Button</a></div>
I'm trying to get button when click in div too, or when click in button, select all div.
In components, I tried:
domComps.addType('link', {
model: linkType.model.extend({
defaults: Object.assign({}, linkType.model.prototype.defaults, {
traits: [
{
label: 'Link',
name: 'href',
placeholder: 'http://mywebsite.com'
},
{
label: 'Content',
type: 'input-content',
placeholder: 'Button'
}
]
}),
},{
isComponent: function(el, sec) {
if(el.tagName == 'A'){
return {type: 'link'};
}
// I already tried to return always link:
// return {type: 'link'};
},
})
});
Related Questions and Answers
Continue research with similar issue discussions.
Issue #1994
[question] Is it possible to add StyleManager property for only class name
Thank for this awesome library. I am gonna to add style controller to sector. This style will be for only one class name and won't set targ...
Issue #921
[QUESTION] Select and focus an html element from a panel button
Hi everyone, Is it possible to set focus in a section html with Grapes, like as an anchor? I have a panel button and it just select the sec...
Issue #792
Script property using function don't work with blocks
In Components & JS docs there's this example: This works perfectly. But I couldn't use a function instead of a string in script, as is said...
Issue #1661
I've created html table and cannot drag new elements to table cells
Hello. if I create html table with next block of code, everything works, I'm able to drop a Quote, for example, to any cell: But next code...
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.
Browse Plugin Categories
Jump directly to plugin category pages on the marketplace.