views:

66

answers:

3

Hi all,

The title might be a bit confusing, basically I'm trying to write up a script which will grab the selected values from a drop down form, and hide li's if its class does not contain the values from the drop down form.. if that makes sense?? Each li has multiple classes..

Here's what I've got so far (sorry if it's crude):

FORM:

<form action="" name="filter" id="filter" method="post">
        <select name="bedrooms">
          <option value="">- Select Bedrooms -</option>
          <option value="bed-1">1</option>
          <option value="bed-2">2</option>
          <option value="bed-3">3</option>
          <option value="bed-4">4</option>
          <option value="bed-5">5</option>
        </select>
        <select name="bathrooms">
          <option value="">- Select Bathrooms -</option>
          <option value="bath-1">1</option>
          <option value="bath-2">2</option>
          <option value="bath-3">3</option>
          <option value="bath-4">4</option>
          <option value="bath-5">5</option>
        </select>
        <select name="frontage">
          <option value="">- Select Frontage Size -</option>
          <option value="frontage-100">100</option>
          <option value="frontage-200">200</option>
          <option value="frontage-300">300</option>
          <option value="frontage-400">400</option>
          <option value="frontage-500">500</option>
        </select>
        <input type="submit" name="filter-submit" id="filter-submit" value="Go" />
      </form>

JQuery:

$("#filter-submit").click(function() {

          var foo = [];
          $("#filter :selected").each(function(i, value){
            foo[i] = $(value).val();
          });
          if (foo) {
            $.each(foo, function(index, value){
              if (value) {
                //hide other objects based on "value"
                //$("#portfolio li").hasClass();
            };
            });
          };

        return false;
        });

Ok so where I'm stuck is how to hide all "#portfolio li"'s which don't have the class which is outputted as "value". I thought I could use hasClass but not sure how to reverse it.. if that makes sense? Any help would be appreciated :)

+2  A: 

You could use .not() and pass the class, like this:

$("#filter-submit").click(function() {
  var lis = $("#portfolio li");          //get all of the <li> elements
  $("#filter select").each(function() {  //loop through selected classes
    var val = $(this).val();             //get selected value, could be blank
    if(val) lis = lis.not('.' + val);    //get only <li>s without the class
  });
  lis.hide();                            //hide the ones without all classes
  return false;                          //prevent default behavior
});
Nick Craver
Thanks Nick, I've managed to use the :not selector in my script and get it working, but for some reason I can't get your script to work? It just hides everything, not sure why :S
SoulieBaby
$("#portfolio li").not("."+value).hide(); is the code I used where i had the hashed out comments and it works fine, my code is just horribly messy lol
SoulieBaby
@SoulieBaby - The initial selector was missing a `select` on the end...updated for a more compact version overall though, give it a go, not where I can test at the moment, apologies for that.
Nick Craver
Ahh got it working now! Thank you muchly :)
SoulieBaby
@SoulieBaby - welcome :)
Nick Craver
+1  A: 
$("#filter-submit").click(function()
{
    var Portfolio = $("#portfolio");
    $('li',Portfolio).show();

    $("#filter selectb :selected").each(function(i){
        Class = $(this).attr('value');
        $('li',Portfolio).not('.' + Class).get().hide();
    });
    return false;
});

give that a go!

RobertPitt
The selector `select:selected` won't find anything...it needs a space, e.g. `select :selected` :)
Nick Craver
+1  A: 

Just thought I'd throw this one out there for fun. Takes a little different approach to hiding the <li> elements. Does it all in one selector.

$("#filter-submit").click(function() {
    var value = $('#filter select').map(function() {
        return ($(this).val() || null);
    }).get().join( '):not(.' );

    if(value) $('#portfolio li:not(.' + value + ')').hide();// Only filter if at
});​                                                         // least one selected

The value variable will hold a String something like this:

"bed-1):not(.bath-1):not(.frontage-100"
patrick dw