0

I am working on simple filtering images project with jquery I have a data-attribute called "data-genre" which contains multiple values

Ex: data-genre = "sci-fi, police, mystery, psychological"

when my data-genre has single value like data-genre = "action" am able to filter the images which contain action genre (through selecting action genre in select box) but as you know an anime can have multiple genres so when I add multiple values like above example it shows nothing I want my jquery code in such a way that when I select police genre in select box it should loop through those multiple values of data-genre and display the images with police genre

This is my source code

HTML CODE:


<!DOCTYPE html>
<html lang="en">
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>


<title>Anime Filter</title>
<style>
*{
margin: 0 auto;
padding: 0 auto
box-sizing: border-box;
}

.an-filterbar{
text-align: center;
margin: 50px;
}

.ps-page{
margin: 100px;
}

.ps-page .ps-links{
margin-left: 30px;
float: left;
text-align: center;
}

</style>
</head>
<body>
<div class="an-filterbar">
    <label for="an-search"><b>Type :</b></label>
      <select class="type">
        <option value="all">All</option>
        <option value="tvseries">Tv series</option>
        <option value="movie">Movie</option>
      </select>
        <br>
    <label for="an-search"><b>Genre :</b></label>
      <select class="genre">
        <option value="all">All</option>
        <option value="action">Action</option>
        <option value="adventure">Adventure</option>
        <option value="cars">Cars</option>
        <option value="comedy">Comedy</option>
        <option value="police">Police</option>
      </select>
      <br>
     <label for="an-search"><b>Year :</b></label>
      <select class="year">
        <option value="all">All</option>
        <option value="1990">1990</option>
        <option value="1991">1991</option>
        <option value="1992">1992</option>
        <option value="1993">1993</option>
        <option value="1994">1994</option>
      </select>
      <br>
     <label for="an-search"><b>Season :</b></label>
      <select class="season">
        <option value="all">All</option>
        <option value="winter">Winter</option>
        <option value="spring">Spring</option>
        <option value="summer">Summer</option>
        <option value="fall">Fall</option>
      </select>
      <br>
    <label for="an-search"><b>Status :</b></label>
      <select class="status">
        <option value="all">All</option>
        <option value="airing">Airing</option>
        <option value="finished">Finished</option>
        <option value="notyetaired">Not yet Aired</option>
      </select>
      <br>
</div>

<div class="ps-page">
<div class="ps-links" data-type="movie" data-genre="adventure, comedy, action" data-year="1990" data-season="summer" data-status="notyetaired">
  <a href="">
  <figure>
  <img src="assets/pimg-6.jpg" alt="Eizouken ni wa te wo dasu na">
  <figcaption>
  <b>Rating : N/R</b><br>
  Eizouken ni wa te wo dasu na
  </figcaption>
  </figure>
  </a>
  </div>
  <div class="ps-links" data-type="tvseries" data-genre="adventure, cars" data-year="1991" data-season="fall" data-status="airing">
  <a href="">
  <figure>
  <img src="assets/pimg-7.jpg" alt="Somali to mori no kamisama">
  <figcaption>
  <b>Rating : N/R</b><br>
  Somali to mori no kamisama
  </figcaption>
  </figure>
  </a>
  </div>
  <div class="ps-links" data-type="tvseries" data-genre="action, comedy, cars" data-year="1992" data-season="summer" data-status="finished">
  <a href="">
  <figure>
  <img src="assets/pimg-8.jpg" alt="Jibaku shounen hanako-kun">
  <figcaption>
  <b>Rating : N/R</b><br>
  Jibaku shounen hanako-kun
  </figcaption>
  </figure>
  </a>
  </div>
  <div class="ps-links" data-type="movie" data-genre="sci-fi, police, mystery, psychological" data-year="1993" data-season="spring" data-status="finished">
  <a href="">
  <figure>
  <img src="assets/pimg-9.jpg" alt="Plunderer">
  <figcaption>
  <b>Rating : N/R</b><br>
  Plunderer
  </figcaption>
  </figure>
  </a>
  </div>
  </div>
<script>
$("select.type, select.genre, select.year, select.season, select.status").change(updateCategories);

function updateCategories() {
  //get all the values
  var caType = $('select.type').val();
  var caGenre = $('select.genre').val();
  var caYear = $('select.year').val();
  var caSeason = $('select.season').val();
  var caStatus = $('select.status').val();

  $('.ps-page')
    .find('.ps-links')
    .hide()
    .filter(function () {
      var okcaType = true;
      if(caType !== "all"){
        okcaType = $(this).attr('data-type') === caType;
      }

      var okcaGenre = true;
      if(caGenre !== "all"){
        okcaGenre = $(this).attr('data-genre') === caGenre;
      }

      var okcaYear = true;
      if(caYear !== "all"){
        okcaYear = $(this).filter('data-year') === caYear;
      }

      var okcaSeason = true;
      if(caSeason !== "all"){
        okcaSeason = $(this).attr('data-season') === caSeason;
      }

      var okcaStatus = true;
      if(caStatus !== "all"){
        okcaStatus = $(this).attr('data-status') === caStatus;
      }

      //only fade a image if it satisfies all five conditions
      return okcaType && okcaGenre && okcaYear && okcaSeason && okcaStatus;
  }).fadeIn('fast');
}
</script>
</body>
</html>

try this code and select one of genre in select box it dosent display any images

I want solution in such a way that it should fit inside my code (without changing whole code) and explanation of how it works.

4
  • There is not <select> dropdown in the code. Can you please check if you posted correct code in your question? Commented Apr 19, 2020 at 7:37
  • yes i haven't added select tag because its too big of a list Commented Apr 19, 2020 at 7:50
  • i hope genre select tag helps ansewring my question Commented Apr 19, 2020 at 7:58
  • Can you put it live somewhere because I wanted to see the whole code? If possible. Also in your actual code there are all 5 select tags, right? Commented Apr 19, 2020 at 8:01

1 Answer 1

2

You need to split the data-genre attribute in to an array and check whether the selected Genre exist in that array.

if (caGenre !== "all") {
                    var genreArray = $(this).attr('data-genre').split(",").map(function (item) { return item.trim(); })
                    okcaGenre = genreArray.indexOf(caGenre) !== -1;
                }

Here as you can see, I have splitted the data-genre string to an array and removed surrounding spaces of each item.

Updated code:

function updateCategories() {
        //get all the values
        var caType = $('select.type').val() || 'all';
        var caGenre = $('select.genre').val() || 'all';
        var caYear = $('select.year').val() || 'all';
        var caSeason = $('select.season').val() || 'all';
        var caStatus = $('select.status').val() || 'all';


        $('.ps-page')
            .find('.ps-links')
            .hide()
            .filter(function () {
                var okcaType = true;
                if (caType !== "all") {
                    okcaType = $(this).attr('data-type') === caType;
                }

                var okcaGenre = true;
                if (caGenre !== "all") {
                    var genreArray = $(this).attr('data-genre').split(",").map(function (item) { return item.trim(); })
                    okcaGenre = genreArray.indexOf(caGenre) !== -1;
                }

                var okcaYear = true;
                if (caYear !== "all") {
                    okcaYear = $(this).filter('data-year') === caYear;
                }

                var okcaSeason = true;
                if (caSeason !== "all") {
                    okcaSeason = $(this).attr('data-season') === caSeason;
                }

                var okcaStatus = true;
                if (caStatus !== "all") {
                    okcaStatus = $(this).attr('data-status') === caStatus;
                }

                //only fade a image if it satisfies all five conditions
                return okcaType && okcaGenre && okcaYear && okcaSeason && okcaStatus;
            }).fadeIn('fast');
    }
Sign up to request clarification or add additional context in comments.

2 Comments

i just made changes to my genre jquery code that you prescribed in first code box (three lines code) it worked out . but when i tried to copy that whole updated code it dosen't show any changes at all may be because of adding "all" value at the end of variables
Since you haven't added other select elements in your code, I added "all" to avoid errors. You are most welcome.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.