0

What the following code does is take the selected genre genre and check if it contains one of the data which are all arrays.

Example:

genre = "comedy";
data = ["action", "comedy"]

I just want to check if data contains "comedy":

$( "header section" ).selectable({
    selected: function() {
        var genre = $(".ui-selected").data("genre");
        console.log(genre);
        $("body > section div").each(function() {
            var data = $(this + ":contains('" + genre + "')").data("genre");
            if (data != genre) {
                $(this).css("display", "none");   
            } else {
                $(this).css("display", "block");
            }
    });
    } 
});

I tried to do it with the Selector "contains" but it doesn't work with this I think.

digitalextremist
  • 5,952
  • 3
  • 43
  • 62
visionInc
  • 175
  • 4
  • 11

2 Answers2

1

You can do this:

$( "header section" ).selectable({
   selected: function() {

      var genre = $(".ui-selected").data("genre");
      var data = $("body > section div").map(function() {
                      return $(this).data("genre");
                  }).get(); // creates an array

      if($.inArray(genre, data) !== -1){//<--check if 'genre' exists in 'data'
          $(this).css("display", "block");
      }else{
          $(this).css("display", "none");
      }
   } 
});

About $.inArray():

  1. if elem is found returns the index of found elem.
  2. if not then it will return -1.
Jai
  • 74,255
  • 12
  • 74
  • 103
0

You can use Array.indexOf() or use $.inArray() like this:

if( $.inArray(genre, data) > -1 ){
    alert('contains')
}
digitalextremist
  • 5,952
  • 3
  • 43
  • 62
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531