Issue #1389💬 AnsweredOpened August 28, 2018by mariselvanm0 reactions

Add Attribute to Child Elements in component

快速解答by artf

Check here all the methods available for components: https://grapesjs.com/docs/api/component.html Btw you would something like this

Read full answer below ↓

Question

This is the component I try to add in Editor.

`

  <div class="page">

            <div class="col1"></div>

            <div class="col2"></div>

         </div> 

`

After adding those components I try to add some computed values to the elements like this,

`

element.on('component:add',function(ele){

      ele.addAttributes({

      'data-top' : topOffset,

      'data-left' : leftOffset,

    });

editor.render();// For render Updated html

});`

But, this code only update the .page component only.

How Can I add those attr into .col1,.col2 elements inside .page component ?

Answers (2)

artfSeptember 2, 2018

Check here all the methods available for components: https://grapesjs.com/docs/api/component.html

Btw you would something like this

element.on('component:add', component => {
	component.find('.col1, .col2').forEach(comp => {
		comp.addAttributes({ ... });
	});
	
	// YOU DON'T NEED TO RE-RENDER
	// editor.render();
});
lock[bot]September 17, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Related Questions and Answers

Continue research with similar issue discussions.

Paid Plugins That Match This Issue

Curated by issue keywords and label relevance to help you ship faster.

View all plugins

Loading paid plugin recommendations...

Free option

Check the open-source GrapesJS plugins on GitHub or run a quick search in our free catalog.

Browse free plugins →
Premium option

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.

All tutorials →

Browse Plugin Categories

Jump directly to plugin category pages on the marketplace.