A
A
Alexey102019-02-20 19:32:54
Programming
Alexey10, 2019-02-20 19:32:54

How to set parent element for tags?

Hello everyone)
There is such an arrangement of html blocks.
5c6d80cf15cbe921528220.png

How to add a parent using jquery (or js) to those divs that are after the div with the .block class. That is, like this:
5c6d8121b6eab331524158.png

My code:

$(".block").nextAll().each(function(index, el) {
  var parent = "<div class="parent"></div>"

  $(this).nextAll().wrapAll(parent);
  
});


Thanks for the help)

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
abcd0x00, 2016-09-07
@abcd0x00

Why in this fragment it is required to create an additional. Count method?

When you call it, a closure is created that accumulates a value in the variable sum each time it is called. And such closures can be made several independent of each other.
CountIt c1 = Counter();
CountIt c2 = Counter();
CountIt c3 = Counter();

That's what they are used for. Between calls, something is stored in a variable, and for each closure, this variable is its own.

A
Alexander Pozharsky, 2016-09-07
@alex4321

When you call counter - the variable sum is created and the function working with it is returned.
Those. after the Counter call - sum == 0.
After the first delegate call - sum == 0 + (1+2+3) = 6
After the second delegate call - sum == 6 + (1+2+3+4+5) = 21

0
0xD34F, 2019-02-20
@Alexey10

$('.block').each(function() {
  $(this).nextUntil('.block').wrapAll('<div class="parent"></div>');
});

T
tyzberd, 2019-02-20
@tyzberd

if you add a class for those divs that you need to wrap,
then first use .nextAll() https://api.jquery.com/nextAll/
and then .wrapAll()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question