Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <!------ 絞り込み部分 ------>
<div class="bg-light p-3 border-bottom mb-3">
  <div class="container-fluid">
    <div class="row">
      <div class="col-6">
        <select class="form-select js-filter" name="color">
          <option value="all">全色</option>
          <option value="赤">赤</option>
          <option value="青">青</option>
          <option value="緑">緑</option> 
        </select>
      </div>
      <div class="col-6">
        <select class="form-select js-filter" name="size">
          <option value="all">全サイズ</option>
          <option value="S">S</option>
          <option value="M">M</option>
          <option value="L">L</option> 
        </select>
      </div>
    </div>
  </div>
</div>

<!------ 結果メッセージ ------>
<p class="px-3">検索結果:<span class="result-msg"></span></p>

<!------ アイテム一覧 ------>
<div class="items">
	<div class="item bg-danger" data-color="赤" data-size="S">赤 / S</div>
  <div class="item bg-primary" data-color="青" data-size="M">青 / M</div>
  <div class="item bg-success" data-color="緑" data-size="M">緑 / M</div>
  <div class="item bg-danger" data-color="赤" data-size="L">赤 / L</div>
  <div class="item bg-primary" data-color="青" data-size="M">青 / M</div>
  <div class="item bg-success" data-color="緑" data-size="S">緑 / S</div>
  <div class="item bg-danger" data-color="赤" data-size="S">赤 / S</div>
  <div class="item bg-primary" data-color="青" data-size="L">青 / L</div>
  <div class="item bg-success" data-color="緑" data-size="S">緑 / S</div>
  <div class="item bg-danger" data-color="赤" data-size="L">赤 / L</div>
  <div class="item bg-primary" data-color="青" data-size="M">青 / M</div>
  <div class="item bg-success" data-color="緑" data-size="L">緑 / L</div>
  <div class="item bg-danger" data-color="赤" data-size="L">赤 / L</div>
  <div class="item bg-primary" data-color="青" data-size="S">青 / S</div>
  <div class="item bg-success" data-color="緑" data-size="L">緑 / L</div>
  <div class="item bg-danger" data-color="赤" data-size="M">赤 / M</div>
  <div class="item bg-danger" data-color="赤" data-size="M">赤 / M</div>
  <div class="item bg-success" data-color="緑" data-size="S">緑 / S</div>
</div>
              
            
!

CSS

              
                /***** 非表示:Bootstrap使用時は不要 *****/
.d-none {
  display: none;
}

/***** 見た目調整(任意) *****/
.items {
  display: grid;
  gap: 1rem;
  grid-template-columns: repeat(2, 1fr);
  margin: 1rem;
}

.item {
  padding: 1rem;
  text-align: center;
  color: #fff
}
              
            
!

JS

              
                function multi_filter(h, t, s, r = '')
{
  //****** 変数定義 ******
  let hidden = h;
  let target = t;
  let targets = document.querySelectorAll(target);
  let selects = document.querySelectorAll(s);
  let result = document.querySelector(r);
  let result_msg = '';
  
  if (selects) {
    let selects_length = selects.length;
    //****** セレクトボックスごとにchangeイベントをチェック ******
    for (let i = 0; i < selects_length; i++) {
      selects[i].addEventListener('change', () => {
        let targets_length = targets.length;
        let hidden_num = 0;
        //*** targetを順番に調べる ***
        for (let ii = 0; ii < targets_length; ii++) {
          //*** 非表示クラスを一旦削除 ***
          targets[ii].classList.remove(hidden);
          //*** セレクトボックスの数だけ総当たりで検索 ***
          for (let iii = 0; iii < selects_length; iii++) {
            //*** selectのname値 / optionのvalue値 / targetの文字列を取得 ***
            let value = selects[iii].value;
            let name = selects[iii].getAttribute('name');
            let item_data = targets[ii].getAttribute('data-' + name);
            //*** value値が存在&all以外&一致しない / 非表示クラスなしのとき:非表示クラスを付与 ***
            if (value && value !== 'all' && value !== item_data && !targets[ii].classList.contains(hidden)) {
              targets[ii].classList.add(hidden);
              hidden_num++;
            }
          }
        }
      
        //****** 結果メッセージ ******
        if (result) {
          //*** 該当なし ***
          if (hidden_num === targets_length) {
            result_msg = '見つかりませんでした。';
          }
          //*** 該当あり ***
          else {
            let result_num = targets_length - hidden_num;
            result_msg = result_num + '件見つかりました。';
          }
          result.innerText = result_msg;
        }
      });
    }
  }
}

//*** 関数呼び出し ***
multi_filter('d-none', '.item', '.js-filter', '.result-msg');


/**
 * note:
 *
 * 複数項目対応と銘打ってはいるけれど、当人は2つまでしか試したことがない
 * 3つ以上に増やしたときの挙動は果たしてどうなるのか
 *
 * 27行目:
 * datasetでは変数が使えないようなのであえてgetAttributeにした
 * 
 * 注意点として、以下を揃えないと上手く動かない
 * selectのname属性値 / 対象要素のdata-○○部分
 * optionのvalue値 / 対象要素のdata属性値
 *
 * hidden     : 非表示クラス名
 * target     : 対象要素のクラス名
 * targets    : 全ての対象要素
 * selects    : 全ての対象セレクトボックス
 * - 以下は任意 -
 * result     : 結果テキストの表示場所
 * result_msg : 結果テキスト
 */

              
            
!
999px

Console