views:

48

answers:

2

(A jQuery Question about filtering stuff on StackOverflow. I swear, it doesn't belong on Meta!)

Question
I am looking to use a Greasemonkey script to filter the questions on the Home, Questions, and Unanswered Page. Right now I have a solution for it (below), but it takes a few seconds to process, and I was wondering if there is a better way.

Background
If you haven't taken a look at the structure of the home page, it is very similar to this:
(simplified for question purposes)

<div class="question-summary"> 
    <div class="status">
       <div class="mini-counts">1</div>
       <div>answers</div>
    </div>
</div>

and this is the structure of the questions/unanswered page:

<div class="question-summary"> 
    <div class="status">
       <strong>1</strong>
       answers
    </div>
</div>

Now, I need to grab the '1' (the number of answers) out of each question, and check if it is above a certain number. Currently I'm using this code:

function filterByAnswer(number)
{
    $.each($('.question-summary'), function()
    {
            // 
 if($('.status .mini-counts',this))
 {
  var answers = $('.status .mini-counts',this).text();
 }
 else if($('.status strong',this))
 {
  var answers = $('.status strong',this).text();
 }
 if(answers > number)
 {
  $(this).hide();
 }  
});
}

My Question is, is there a faster way to do this? I found that this take a few seconds to process, and would like a snappier solution.

Thanks for any help you can provide!

A: 

You could reuse the values of $('.status .mini-counts',this) and $('.status strong',this).

 var $miniCounts = $('.status .mini-counts',this);

 if($miniCounts)
    {
            var answers = $miniCounts.text();
    }
    else 
    {
       var $strong = $('.status strong',this);
       if($strong) {
            var answers = $strong.text();
       }
    }
Patrick McElhaney
For some reason that really slowed it down .. strange.
Chacha102
+1  A: 

Try this:

function filterByAnswer(number) 
{
    $('.question-summary').filter( 
        function () { 
            var answers = $(this).find('.status').children('.mini-counts, strong').text(); 
            return parseInt(answers, 10) < number; 
        }).hide();       
}
Patrick McElhaney
Yes! Thats exactly what I needed!
Chacha102