Tuesday, May 8, 2012

Hover event stops working after sorting divs

I'm working on an application which renders out images side by side. When hovering an image a hidden class is revealed showing information of the image.



I also have a sort function so that one can sort the images by points (highest first, descending) instead of the default sort (date). Both hovering and sorting works fine, except for one thing.



When the images is sorted by points and rendered out again, hovering the images doesn't work. I have looked at the generated code and it looks the same, but regardless that it doesn't work.



I would really appreciate som help here, thanks!



<div id="images">
<div class="image">
<div class="post">
// other code here
<div class="postDesc">
// other code here
<p class="postDescContent">Points: 10</p>
</div
</div>
</div>
<div class="image">
<div class="post">
// other code here
<div class="postDesc">
// other code here
<p class="postDescContent">Points: 20</p>
</div
</div>
</div>
// and so one...
</div>


sorting function:



sortByPoints: function() {
var contents = $('div#images div[class^="image"]');

contents.sort(function(a, b) {

var p1= parseInt($('p.postDescContent', $(a)).text().replace('Points:','').trim()),
p2 = parseInt($('p.postDescContent', $(b)).text().replace('Points:','').trim());
return p1 < p2;

});

var images = $('#images');

images.empty();
images.append(contents);
}


hover function:



$('.image').hover(function () {
$(this).find('.postDesc').toggle();
});




No comments:

Post a Comment