I have this code where i want to insert the div after the line in which element is present.I'm able to do do this for the last element in the line. The problem is i'm not able to figure how to do it for rest of the elements in same line
HTML
<div class="wrap">
<div class="info"></div>
<div class="info"></div>
<div class="info"></div>
<div class="info"></div>
<div class="info"></div>
<div class="info"></div>
<div class="info"></div>
<div class="info"></div>
<div class="info"></div>
<div class="info"></div>
<div class="info"></div>
<div class="info"></div>
<div class="info"></div>
<div class="info"></div>
<div class="newdiv"></div>
CSS
.wrap{border:1px solid #000;width:630px}
.info{border:1px solid red; width:180px;float:left;margin-left:20px;height:100px;margin-top:20px}
.newdiv{width:100%;height:100px;background:green;display:none;float:left}
.active{background:brown}
Jquery
$('.info').on('click',function(){
$('.info').removeClass('active');
$(this).addClass('active');
if(($(this).index('.info')+1)%3===0)
{
$('.newdiv').insertAfter($(this)).show();
}
});
Calculate the index of the 3thd element:
http://jsbin.com/AvERUpU/4/