Monday, 19 August 2013

Detecting a class exists and then reordering

Detecting a class exists and then reordering

I want to detect that a div class within a container, that is repeated,
exists. I then want to detect if that div class sits before another div
class. If it does then I want to move that div to sit after the other div.
<div class="container">
<div class="column1">
<div class="column2">
</div>
<div class="container">
<div class="column1">
<div class="column2">
</div>
<div class="container">
<div class="column2">
<div class="column1">
</div>
So, in the above example I wish to detect whether column 1 exists and if
it sits before or after column 2.
If it sits before column 2 I want to move it to sit after column 2.
I have tried to determine the length/index of the divs, but to no avail.
This is my latest attempt. I am a newbie at jQuery so forgive me:
$('.container').find('.column1').siblings().each(function(){
var index = $(this).index();
var index2 = $('column2').index();
if (index > $('column2').index()){
$(this).css('background','#000');
} else {
(this).css('background','#dedede');
}
});

No comments:

Post a Comment