I believe the best way is to walk through the child nodes of your element and treat those who possess nodeType
equal to 8, referring to COMMENT_NODE
, to identify which are comments.
const container = document.querySelector('.container')
const COMMENT_NODE = 8
for (let element of container.childNodes) {
if (element.nodeType !== COMMENT_NODE) {
continue;
}
console.log(element.nodeValue);
}
<div class="container">
<!-- [p] -->texto 1<!-- [/p] -->
<!-- [p] -->texto 2<!-- [/p] -->
<!-- [p] -->texto 3<!-- [/p] -->
</div>
So you can already get the elements you want and their contents, ' [p] '
and ' [/p] '
. After that, you just need to treat these values to generate the desired element, possibly replacing [
for <
and ]
for >
and updating the DOM with the new elements.
Why and how you intend to do this?
– Leandro Angelo
Who gave
-1
could leave a comment to help improve the question. I can agree that it is good practice for the author of the question to show what he has already tried... otherwise I find the question interesting and relevant.– Sergio
Do you want this content to appear? because it’s "Hidden"?
– Yuri Dias
I see how to retrieve these comments by Ode... but how would you take the value between each comment and rebuild the gift? you who assembles html in this way?
– Leandro Angelo