Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

MooseSaid's avatar

How can I use an HTML tag to wrap all elements that follow a specified element?

I'm working with Vue 2 and Sanity.io and I need a way to surround all elements that comes after a specific element with HTML tag, and then surround this element along with the followed elements with another HTML tag.

let's say every <h1> will have multiple paragraphs that follows it and then another <h1> and another paragraphs follow it. I want those to be divided with summary and details HTML tags.

For example, I have data coming over from Sanity and it goes like this.

<h1>text</h1>
<p>text</p>
<p>text</p>
<p>text</p>

<h1>text</h1>
<p>text</p>
<p>text</p>
<p>text</p>

I need a way to manipulate the DOM to make it read my data like this

<details>
<summary><h1>text</h1></summary>
<p>text</p>
<p>text</p>
<p>text</p>
</details>

<details>
<summary><h1>text</h1></summary>
<p>text</p>
<p>text</p>
<p>text</p>
</details>

To clarify more, I'm using protable-text-to-vue package which sends back the data as vue components blocks. basically they are coming from sanity in a similar form to the example mentioned above.

0 likes
3 replies
vincent15000's avatar

Why don't you use the semantic tags section or article to wrap your content ? If you need, you can also add a specific class to these tags.

<section class="details">
	...
</section>

Or ...

<article class="details">
	...
</article>
vincent15000's avatar

@MooseSaid Just a little comment : you can use only one <h1> tag per page.

I have read your post again. In fact you need to handle the incoming datas to obtain the result with the details and summary tags.

Well ... you will need to handle this manually.

I suggest you to parse your incoming datas with an HTML parser.

Never used, but here is one.

https://www.npmjs.com/package/node-html-parser

Then you can retrieve each part of your datas and wrap them. For example retrieve the <h1> tags and wrap them inside a <summary> tag.

Please or to participate in this conversation.