Home > Mobile >  How to use JQuery to Wrap a specific list of div children
How to use JQuery to Wrap a specific list of div children

Time:01-28

I am doing AB testing in google optimize. I need to wrap div from number 5 to 10 but I have not been able to do it:

<div  data-order="1">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
<div>7</div>
<div>8</div>
<div>9</div>
<div>10</div>
<button>11</button>
<div>12</div>
</div>

I am using this code to do it but it start wrapping from div number one but I want to start with div number 5 until 10

$('.gsw_cf.step.active > div:lt(#)').wrapAll('<div />');

anyone knows what should I add?

Thank you

CodePudding user response:

You can use jQuery's slice();

$('.active > div').slice(5,10).wrapAll('<div />');

$('.active > div').slice(5,10).wrapAll('<div />');
.wrapped{
background:red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div  data-order="1">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
<div>7</div>
<div>8</div>
<div>9</div>
<div>10</div>
<button>11</button>
<div>12</div>
</div>

CodePudding user response:

I found a solution that works where i use this function:

$('div[data-order="1"]').each(function(){
    $(this).children(".input_wrapper.text").wrapAll("<div class='wrapper' />");
});

I found that solution in http://jsfiddle.net/QWHYK/ and Wrapping sets of elements from a list in DIVs using jQuery

There is also, another solution in How to wrap multiple divs

  •  Tags:  
  • Related