How to wrap every 2 elements inside a container in a div, except the ones with a certain class out?

Vento Aureo

I want to group every 2 images in a different div but leaving the wider ones with only one image. That's the HTML:

<figure id="container">
    <img src="001.jpg" class="single" />
    <img src="002.jpg" class="single" />
    <img src="003.jpg" />
    <img src="004.jpg" />
    <img src="005.jpg" />
    <img src="006.jpg" />
    <img src="007.jpg" />
    <img src="008.jpg" class="single" />
    <img src="009.jpg" />
    <img src="010.jpg" class="single" />
</figure>

So, I don't know what jQuery scripts to use to group these respecting the class "single", the wider ones. I want:

<figure id="container">
  <div><img src="001.jpg" class="single" /></div>
  <div><img src="002.jpg" class="single" /></div>
  <div><img src="003.jpg" />
    <img src="004.jpg" /></div>
  <div><img src="005.jpg" />
    <img src="006.jpg" /></div>
  <div><img src="007.jpg" /></div>
  <div><img src="008.jpg" class="single" /></div>
  <div><img src="009.jpg" /></div>
  <div><img src="010.jpg" class="single" /></div>
</figure>
Dave

Now that I fully understand your question, there's a function below that should work to suit your needs.

Here' what the function does:

  1. Loop through all images in your container and check each if they have class single.
  2. If not single then check the next element too. Wrap just the first one, or both if the second one is also not a single.
  3. Continue through loop, but skip the next iteration if the second element from above was also a single.

Here's the code with runnable snippet:

$(function() {

  var skip = false;
  $('#container img').each(function() {
    var $this = $(this);
    if (!skip && $this.is(":not(.single)")) {
      var $next = $this.next('img');
      if ($next.is(".single")) {
        skip = false;
        $this.wrapAll("<div/>");
      } else {
        skip = true;
        $this.add($next).wrapAll("<div/>");
      }
    } else {
      skip = false
    }
  });
  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<figure id="container">
    <img src="001.jpg" class="single" />
    <img src="002.jpg" class="single" />
    <img src="003.jpg" />
    <img src="004.jpg" />
    <img src="005.jpg" />
    <img src="006.jpg" />
    <img src="007.jpg" />
    <img src="008.jpg" class="single" />
    <img src="009.jpg" />
    <img src="010.jpg" class="single" />
</figure>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

multiple div with same class but display only ones with image inside

From Dev

How to get elements of specific class inside a div already found in JavaScript?

From Dev

Wrap every 2 WordPress posts inside Bootstrap Row Class

From Dev

Select all elements except a certain class

From Dev

Jquery wrap elements inside div

From Dev

How does Symfony injects its DI Container inside every class that extends the Controller one?

From Dev

How to wrap all elements inside a div after a certain element? (JQuery)

From Dev

How to make text wrap before inner floating elements inside a container

From Dev

How to remove elements of a certain class inside a parent element Jquery

From Dev

How to wrap all elements in each container

From Dev

Wrap div around 2 sequential elements in jQuery

From Dev

Jsoup selector - How to select all <p> elements inside <div> element, except for the last <p> element?

From Dev

how to except all elements css inside div with class?

From Dev

How can i remove all div's and other DOM HTML elements except a particular class using jQuery

From Dev

DIV Wrap two different elements inside a DIV by JS

From Dev

How to wrap elements inside of body dynamically

From Dev

Float 2 elements side by side inside a container div

From Dev

jquery, on click anywhere inside div except certain child

From Dev

Wrap text inside contenteditable div in every row

From Dev

Regex match all elements except ones with certain attribute

From Dev

multiple div with same class but display only ones with image inside

From Dev

How to get elements of specific class inside a div already found in JavaScript?

From Dev

Wrap every 2 WordPress posts inside Bootstrap Row Class

From Dev

Select all elements except a certain class

From Dev

Wrap every immediate group of objects, including text, except divs inside a new div

From Dev

How to wrap <a> elements inside of a table?

From Dev

Get all elements except ones from a certain class?

From Dev

Wrap a div around every 2 elements in jQuery

From Dev

How to wrap all elements in each container

Related Related

  1. 1

    multiple div with same class but display only ones with image inside

  2. 2

    How to get elements of specific class inside a div already found in JavaScript?

  3. 3

    Wrap every 2 WordPress posts inside Bootstrap Row Class

  4. 4

    Select all elements except a certain class

  5. 5

    Jquery wrap elements inside div

  6. 6

    How does Symfony injects its DI Container inside every class that extends the Controller one?

  7. 7

    How to wrap all elements inside a div after a certain element? (JQuery)

  8. 8

    How to make text wrap before inner floating elements inside a container

  9. 9

    How to remove elements of a certain class inside a parent element Jquery

  10. 10

    How to wrap all elements in each container

  11. 11

    Wrap div around 2 sequential elements in jQuery

  12. 12

    Jsoup selector - How to select all <p> elements inside <div> element, except for the last <p> element?

  13. 13

    how to except all elements css inside div with class?

  14. 14

    How can i remove all div's and other DOM HTML elements except a particular class using jQuery

  15. 15

    DIV Wrap two different elements inside a DIV by JS

  16. 16

    How to wrap elements inside of body dynamically

  17. 17

    Float 2 elements side by side inside a container div

  18. 18

    jquery, on click anywhere inside div except certain child

  19. 19

    Wrap text inside contenteditable div in every row

  20. 20

    Regex match all elements except ones with certain attribute

  21. 21

    multiple div with same class but display only ones with image inside

  22. 22

    How to get elements of specific class inside a div already found in JavaScript?

  23. 23

    Wrap every 2 WordPress posts inside Bootstrap Row Class

  24. 24

    Select all elements except a certain class

  25. 25

    Wrap every immediate group of objects, including text, except divs inside a new div

  26. 26

    How to wrap <a> elements inside of a table?

  27. 27

    Get all elements except ones from a certain class?

  28. 28

    Wrap a div around every 2 elements in jQuery

  29. 29

    How to wrap all elements in each container

HotTag

Archive