tags:

views:

67

answers:

3

Hi Guys,

This is kind of a simple question, however, I don't seem to figure out how to do it:

I´ve got a slider filtering some stuff

$("#price").slider(
  {
  range: true,
  step: 5, 
  change: function(e,ui) {
     $('total').filter(function(index) {
      return ( ($("#price").slider("values", 0)) <= $(this).text() <=
                   ($("#price").slider("values", 1)));
     }).parents('div.item').hide();
  }
});

Basically, I want an array with index of each of elements which have been filtered so I can reuse them for other purpose. I was thinking of editing filter function to something like:

$('total').filter(function(index) {
   var matches = ( ($("#price").slider("values", 0)) <= $(this).text() <=
                   ($("#price").slider("values", 1)));
   return matches;
}.myFunction(matches){
//do some stuff here with matched elements 
}

This is not correct, your help is greatly appreciated.

+1  A: 

Update for Comments: You can do this using .map(), like this:

change: function(e,ui) {
  var self = $(this);
  var indexes = [];
  $('.total').filter(function(index) {
      var txt = $(this).text();
      if (self.slider("values", 0) <= txt && txt <= self.slider("values", 1))
      {
        indexes.push(index);
        return true;
      }
  }).parents('div.item').hide();
  //do something with indexes array
}
Nick Craver
Well not exactly,, the filter was working fine (i was just trying to put it simple thus my mistake in the example), the problem is still like to have an array with the numbers I have filtered.
Matias
@Matias - You want an array of the values that matched, or didn't match the slider's current range?
Nick Craver
theo ones which matches, thanks Nick!
Matias
@Matias -Updated, give it a shot :)
Nick Craver
Thanks Nick for your answer. Unfortunately var=values its returning the text within the divs itself but I just want the position of the elements which have been matched.
Matias
@Matias - updated to this...on iPhone though, so untested, hope it works :)
Nick Craver
Not yet, thanks for your help.. this is really closer to what I was looking for but the Array indexes is still empty. Cant figure out why .. :)
Matias
@Matias - Where are you calling indexes??? maybe you are using it outside the scope so thus it's returning empty to you...
Reigel
@Matias - I was able to test this...working here, can you post the code you're using, preferably with relevant markup?
Nick Craver
A: 

Here is the full code I am working on:

$("#price").slider(
            {
            range: true,
            step: 5, 
            change: function(e,ui) { 
            $('span.the_total').filter(function(index) {
                    return ( ($("#price").slider("values", 0)) <= $(this).text() <= ($("#price").slider("values", 1)));
            }).parents('div.item_hotel').show();

            $('span.the_total').filter(function(index) {
                     return ( ($("#price").slider("values", 0)) > $(this).text() );
            }).parents('div.item_hotel').hide();

            $('span.the_total').filter(function(index) {
                     return ( ($("#price").slider("values", 1)) < $(this).text());
            }).parents('div.item_hotel').hide();

            }});
Matias
A: 

Nick,

Sorry mate, your answer works smoothly.. Thanks very much for your great help.

Coding is not for me.. :(

Matias