mddev
mddev

Reputation: 35

Filtering elements with multiple data attributes using jquery

I am trying to figure out how to properly apply a CSS class to an HTML element based on that elements data-attribute using jQuery.

What I am trying to achieve is the following:

  1. Highlight a "store" element based on what "fruit(s)" are selected (i.e. if a banana and apple is selected, all store elements that have bananas and apples will be highlighted.
  2. Select all "store" elements if the "all" option is selected.

The issue I am currently having is once I select more then one "fruit", an element that was previously selected, will be selected even though it has one or more of the selected fruits.

$('a.fruit').click(function() {
  $(this).toggleClass("active");
  var fruits = $(this).data("fruit");
  $("div.store[data-fruits~='" + fruits + "']").toggleClass("active");
});
body {
  margin: auto;
  max-width: 40em;
  font-family: "Segoe UI";
  background-color: #FFFFFF;
}

ul {
  list-style-type: none;
  padding: 0;
}

.fruit.active::after {
  content: "✔️";
  margin-left: .5rem;
}

.store {
  position: relative;
  display: inline-block;
  width: 10rem;
  padding: 1rem;
  border-radius: 1rem;
  border: 1px solid black;
  text-align: center;
  margin: 0 0.5rem 0.5rem 0;
  bottom: 0;
  -webkit-transition: all .2s ease;
  -moz-transition: all .2s ease;
  -ms-transition: all .2s ease;
  -o-transition: all .2s ease;
  transition: all .2s ease;
}

.store span {
  display: block;
  font-weight: bold;
  width: 100%;
  margin-bottom: 1rem;
  text-decoration: underline;
}

.store-container {
  position: relative;
}

.store.active {
  position: relative;
  box-shadow: 3px 3px 5px grey;
  bottom: 5px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
  <h1>
    Fruit Selector:
  </h1>
  <ul>
    <li>
      <a class="fruit" data-fruit="all">🛒 All Fruits</a>
    </li>
    <li>
      <a class="fruit" data-fruit="banana">🍌 Banana</a>
    </li>
    <li>
      <a class="fruit" data-fruit="apple">🍎 Apple</a>
    </li>
    <li>
      <a class="fruit" data-fruit="orange">🍊 Orange</a>
    </li>
    <li>
      <a class="fruit" data-fruit="grape">🍇 Grape</a>
    </li>
    <li>
      <a class="fruit" data-fruit="peach">🍑 Peach</a>
    </li>
  </ul>
  <h1>Stores:</h1>
  <div class="store-container">
    <div class="store" data-fruits="banana apple orange grape peach">
      <span>Store A</span>🍌🍎🍊🍇🍑
    </div>
    <div class="store" data-fruits="banana apple orange grape">
      <span>Store B</span>🍌🍎🍊🍇
    </div>
    <div class="store" data-fruits="banana apple orange">
      <span>Store C</span>🍌🍎🍊
    </div>
    <div class="store" data-fruits="banana apple">
      <span>Store D</span>🍌🍎
    </div>
    <div class="store" data-fruits="banana">
      <span>Store D</span>🍌
    </div>
    <div class="store" data-fruits="apple orange grape peach">
      <span>Store E</span>🍎🍊🍇🍑
    </div>
    <div class="store" data-fruits="orange grape peach">
      <span>Store F</span>🍊🍇🍑
    </div>
    <div class="store" data-fruits="grape peach">
      <span>Store G</span>🍇🍑
    </div>
    <div class="store" data-fruits="peach">
      <span>Store H</span>🍑
    </div>
  </div>
</div>

Please refer to the JSFiddle for my example code so far.

Thank you in advance for any guidance with this issue.

EDIT:

JSFiddle with Funk Doc's solution.

Upvotes: 1

Views: 1056

Answers (1)

Funk Doc
Funk Doc

Reputation: 1643

I used your Fiddle. This might be a little verbose but seems to work. I remove active class from .store each time something is clicked to help fix your problem.

    $('a.fruit').click(function () {
        $(this).toggleClass("active");
        if ($('.fruit[data-fruit="all"]').hasClass('active')) {
            $(".store").addClass("active");
        } else {
            $(".store").removeClass("active");
            $('a.fruit').each(function () {
                if ($(this).hasClass("active")) {
                    var fruit = $(this).data("fruit");
                    $(".store").each(function () {
                        if ($(this).data("fruits").includes(fruit)) {
                            $(this).addClass("active");
                        }
                    });
                }
            });
        }
    });

Upvotes: 1

Related Questions