cocoon-vanilla-js icon indicating copy to clipboard operation
cocoon-vanilla-js copied to clipboard

Getting the actual inserted element

Open james-em opened this issue 2 years ago • 1 comments

Current API Implementation

...
// node is HTML string.
insertionNodeElem.insertAdjacentHTML(insertionMethod, node);
insertionNodeElem.dispatchEvent(
   new CustomEvent('cocoon:after-insert', {detail: node, bubbles: true, cancelable: true})
);

Proposed implementation

...
// node is HTML string.
var inserted = insertionNodeElem.insertAdjacentHTML(insertionMethod, node);
insertionNodeElem.dispatchEvent(
   new CustomEvent('cocoon:after-insert', {detail: [node, inserted], bubbles: true, cancelable: true})
);

This would allow to interact with the inserted element. There is no way to get the generated ID either so it makes finding it harder. We currently need to get the container and check for last element before the add button.

Another fork example I havn't tested https://github.com/dabroz/cocoon-js-vanilla/blob/master/index.js#L118

james-em avatar Aug 11 '22 13:08 james-em

For what it's worth, that support was added in another implementation: https://github.com/dabroz/cocoon-js-vanilla/pull/9/files

james-em avatar Oct 06 '22 18:10 james-em