0

So I have this code:

$('.brandModelWrapperGroup').each(function(){
  var group = $(this).attr('data-model-group')

  var brandModelwrapperdivs = $(this).find(".brandModelWrapper")
  for(var i = 0; i < brandModelwrapperdivs.length; i+=4) {
    brandModelwrapperdivs .slice(i, i+4).wrapAll("<section class='brandModelLineWrapper'></section>")
  }
  var brandModelviewdivs = $(this).find(".brandModelView")
  for(var i = 0; i < brandModelviewdivs.length; i+=4) {
    brandModelviewdivs.slice(i, i+4).wrapAll("<section class='brandModelLineWrapper'></section>")
  }
})

And what it does is it wraps .brandModelWrapper and .brandModelView in .brandModelLineWrapper, but I need them to be wrapped only if both of their attribute data-model-group matches. Kind of like

if($('.brandModelView').attr('data-model-group') == group) { run the loop }

How could it be done?

jsfiddle: http://jsfiddle.net/8wczna75/1/

Xeen
  • 6,955
  • 16
  • 60
  • 111
  • 2
    `$('.brandModelView[data-model-group="' + group + '"]')` – fuyushimoya Jul 02 '15 at 12:16
  • possible duplicate of [jQuery selectors on custom data attributes on HTML5](http://stackoverflow.com/questions/4146502/jquery-selectors-on-custom-data-attributes-on-html5) – Liam Jul 02 '15 at 12:18
  • @fuyushimoya can you please provide the full example? – Xeen Jul 02 '15 at 12:22
  • `brandModelviewdivs.slice(i, i+4).wrapAll("
    ")` does that mean you want to wrap 4 matched item in a wrapper each time? or its a simulate action.
    – fuyushimoya Jul 02 '15 at 12:33
  • @fuyushimoya I want to move every 4 elements to that wrapper, but those elements must also match by `data-model-group`. – Xeen Jul 02 '15 at 12:34
  • And in `brandModelWrapperGroup`, there's many `brandModelWrapper` and `brandModelView`, you want to wrap them separately? – fuyushimoya Jul 02 '15 at 12:36
  • @fuyushimoya no, I want to wrap them together in one wrapper and their `data-model-group` attributes must match. Also there must be no more than 4 from each. – Xeen Jul 02 '15 at 12:38
  • I've created a example for you, with simplified class name though. – fuyushimoya Jul 02 '15 at 13:00
  • Could you please add HTML code to your question. Your current HTML structure, and your expected HTML structure. – Jamie Barker Jul 02 '15 at 13:19

2 Answers2

0

Use this selector: [data-model-group=group]

$('.brandModelWrapperGroup').each(function(){
  var group = $(this).attr('data-model-group')

  var brandModelwrapperdivs = $(this).find(".brandModelWrapper[data-model-group="+group+"]")
  for(var i = 0; i < brandModelwrapperdivs.length; i+=4) {
    brandModelwrapperdivs .slice(i, i+4).wrapAll("<section class='brandModelLineWrapper'></section>")
  }
  var brandModelviewdivs = $(this).find(".brandModelView[data-model-group="+group+"]")
  for(var i = 0; i < brandModelviewdivs.length; i+=4) {
    brandModelviewdivs.slice(i, i+4).wrapAll("<section class='brandModelLineWrapper'></section>")
  }
})
Adam Kaczmarek
  • 146
  • 2
  • 10
0

Ok so I created one example:

Edit: Your altered jsfiddle, with css background to show it wraps.

var brandModelLineCount = 4
$('.brandModelWrapperGroup').each(function(){
    var group = $(this).attr("data-model-group");

    var targets = $(this).find(".brandModelWrapper, .brandModelView").filter('[data-model-group="' +  group + '"]');
    var i, len = targets.length;
    for (i = 0; i < len; i +=  brandModelLineCount) {
        targets.slice(i, i + brandModelLineCount).wrapAll("<section class='brandModelLineWrapper'></section>");
    }
});

$('.container').each(function(){
   var group = $(this).attr('data-group-model')
  
   var targets = $(this).find('.model,.view').filter('[data-group-model="'+  group +'"]');

    var i, len = targets.length;  
    for (i = 0; i < len;  i += 4) {
        targets.slice(i, i+4).wrapAll("<div class='wrapper'></div>")
    }

});
.container {
    width: 500px;
    background-color: gray;
}

.container div{
    display: inline-block;
}

.model {
    color : blue;
}
.view {
    color : green;
}

.wrapper {
    background-color : #ccc;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container" data-group-model="wtf">
    <div class="model" data-group-model="wtf">WTFM</div>
    <div class="model" data-group-model="aaa">AAAM</div>
    <div class="view" data-group-model="wtf">WTFV</div>
    <div class="view" data-group-model="aaa">AAAV</div>
    <div class="model" data-group-model="wtf">WTFM</div>
    <div class="model" data-group-model="aaa">AAAM</div>
    <div class="model" data-group-model="wtf">WTFM</div>
    <div class="model" data-group-model="aaa">AAAM</div>
    <div class="view" data-group-model="wtf">WTFV</div>
    <div class="view" data-group-model="aaa">AAAV</div>
    <div class="model" data-group-model="wtf">WTFM</div>
    <div class="model" data-group-model="aaa">AAAM</div>
    <div class="view" data-group-model="wtf">WTFV</div>
    <div class="view" data-group-model="aaa">AAAV</div>
    <div class="view" data-group-model="wtf">WTFV</div>
    <div class="view" data-group-model="aaa">AAAV</div>
    <div class="model" data-group-model="wtf">WTFM</div>
    <div class="model" data-group-model="aaa">AAAM</div>
    <div class="view" data-group-model="wtf">WTFV</div>
    <div class="view" data-group-model="aaa">AAAV</div>
    <div class="model" data-group-model="wtf">WTFM</div>
    <div class="model" data-group-model="aaa">AAAM</div>
    <div class="view" data-group-model="wtf">WTFV</div>
    <div class="view" data-group-model="aaa">AAAV</div>
    <div class="view" data-group-model="wtf">WTFV</div>
    <div class="view" data-group-model="aaa">AAAV</div>
    <div class="model" data-group-model="wtf">WTFM</div>
    <div class="model" data-group-model="aaa">AAAM</div>
    <div class="view" data-group-model="wtf">WTFV</div>
    <div class="view" data-group-model="aaa">AAAV</div>
    <div class="view" data-group-model="wtf">WTFV</div>
    <div class="view" data-group-model="aaa">AAAV</div>
    <div class="model" data-group-model="wtf">WTFM</div>
    <div class="model" data-group-model="aaa">AAAM</div>
    <div class="view" data-group-model="wtf">WTFV</div>
</div>

I simplified your class and create some fake, so that saves my time of typing, please check if this is what you want.

fuyushimoya
  • 9,715
  • 3
  • 27
  • 34