A
A
Anatoliy Kolesnik2020-04-27 18:07:38
JavaScript
Anatoliy Kolesnik, 2020-04-27 18:07:38

How to wrap everything that comes after a div?

There is a div followed by text and other elements.

<div class="block">Some text</div>
<hr>
More text
<br>
Again more text

How can I wrap everything after div.block including text, hr and br in div.another__block

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2020-04-27
@Snoopak

const block = document.querySelector('.block');
const parent = block.parentNode;
const childNodes = [...parent.childNodes];
const wrapper = document.createElement('div');
wrapper.classList.add('another__block');
wrapper.append(...childNodes.slice(childNodes.indexOf(block) + 1));
parent.append(wrapper);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question