Wrap multiple div elements on same class
Asked Answered
O

5

7

I would like to use jQuery to wrap sets of class elements in a div but can't find the solution.

HTML:

<div class="view-content">

  <div class="first">content</div>
  <div class="first">content</div>
  <div class="second">content</div>
  <div class="third">content</div>
  <div class="third">content</div>

</div>

Desired Result:

<div class="view-content">

  <div class="column">
    <div class="first">content</div>
    <div class="first">content</div>
  </div>

  <div class="column">
    <div class="second">content</div>
  </div>

  <div class="column">
    <div class="third">content</div>
    <div class="third">content</div>
  </div>

</div>
Ozuna answered 5/6, 2012 at 11:17 Comment(0)
L
9

Demo http://jsfiddle.net/kQz4Z/8/

API: http://api.jquery.com/wrapAll/

Added a break line so that you can see the difference here :) http://jsfiddle.net/kQz4Z/10/

code

$(function() {

    $('.first').wrapAll('<div class="column" />')

    $('.second').wrapAll('<div class="column" />')

    $('.third').wrapAll('<div class="column" />')




    alert($('.view-content').html());
});​
Lobectomy answered 5/6, 2012 at 11:26 Comment(3)
@h2ooooooo here you go jsfiddle.net/ZXb4Y/1 :)) Nice one! set it as your answer if you want, cheers.Lobectomy
Guys, and how about mine dynamical? :)Fulllength
@Fulllength Broooo how have you been :P okies +1 to you as well manLobectomy
L
3

Use wrapAll() method

$(function(){
    var classes = ['.first', '.second', '.third'];

    for (i = 0; i < classes.length; i++) {
        $(classes[i]).wrapAll('<div class="column">');
    }​

});

Demo: http://jsfiddle.net/g9G85/

Litigious answered 5/6, 2012 at 11:31 Comment(1)
This is great! Thank you so much!Townie
F
2

Or here is the very short dynamical solution:

​$(".view-content > div").each(function() {
    $(".view-content > ." + this.className).wrapAll("<div class='column' />");
});​

DEMO: http://jsfiddle.net/CqzWy/

Fulllength answered 5/6, 2012 at 11:45 Comment(1)
It should be noted that this method in its current form MUST employ the child combinator selector (>) as shown. Failure to do so will result in a wrapping div for each item that matches a certain className. (For example, in this case, divs with class "third" would be wrapped in TWO divs with class "column").Veinlet
A
1

You can use .wrap() to wrap something in a div but if your content is not ordered it will become a mess, here's an example:

Input

<div class="view-content">
    <div class="first">content</div>
    <div class="second">content</div>
    <div class="first">content</div>
</div>

Output

<div class="view-content">
    <div class="column">
        <div class="first">content</div>
        <div class="column">
            <div class="second">content</div>
        </div>
        <div class="first">content</div>
    </div>
</div>
Arundell answered 5/6, 2012 at 11:25 Comment(0)
A
0

You could try whit this:

var arr = $(".view-content div").map(function() { return $(this).attr('class'); }).get();
var results = $.unique(arr);
var i;

for(i = 0; i < results.length; i++){

    $('.out').append('<div class="columns"></div>');
    $('.'+results[i]).clone().appendTo('.columns:last');
}

alert($('.out').html());

Here an example:

JSFIDDLE

Aronson answered 5/6, 2012 at 13:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.