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

              
                <form id="search" name="search">
  色
  <select name="color" id="color">
    <option value="">指定しない</option>
    <option value="red">赤</option>
    <option value="blue">青</option>
    <option value="green">緑</option>
  </select>

  形
  <select name="type" id="type">
    <option value="">指定しない</option>
    <option value="square">四角</option>
    <option value="circle">丸</option>  
  </select>
 
  <input type="submit" value="検索">
</form>

<div id="items"></div>
              
            
!

CSS

              
                body { padding: 10px }

#search {
  margin: 10px 10px 20px;
  select {
    margin-right: 10px;
  }
}

.item {
  float: left;
  margin: 10px;
  width: 100px;
  height: 100px;
  text-align: center;
  line-height: 100px;
  color: #fff;
}

.red {
  background-color: #e74c3c;
}
.blue {
  background-color: #3498db;
}
.green {
  background-color: #2abd68;
}
.circle {
  border-radius: 50%; 
}
              
            
!

JS

              
                // データを定義
var items = [
  {
    "name"  : "赤い四角",
    "color" : "red",
    "type"  : "square"
  },
  {
    "name"  : "赤い丸",
    "color" : "red",
    "type"  : "circle"
  },
  {
    "name"  : "青い四角",
    "color" : "blue",
    "type"  : "square"
  },
  {
    "name"  : "青い丸",
    "color" : "blue",
    "type"  : "circle"
  },
  {
    "name"  : "緑の四角",
    "color" : "green",
    "type"  : "square"
  },
  {
    "name"  : "緑の丸",
    "color" : "green",
    "type"  : "circle"
  }
];


// 検索が押された時の処理
$('#search').on('submit' , function(event){
  // デフォルトのイベントをキャンセル
  event.preventDefault();

  // 検索項目のオブジェクトを作成してセレクトボックスの値を格納
  var query = {};
  if($('#color').val() != ''){
    query.color = $('#color').val();
  }
  if($('#type').val() != ''){
    query.type = $('#type').val();
  }

  // データの中から一致するオブジェクトを検索
  var results = _.where(items, query);

  // 返ってきた配列で出力処理
  outputResults(results);
});


// 検索結果の出力処理
function outputResults(results){
    // 変数の初期化
    var html = '';

    // 受け取った配列をループで処理
    // 出力するHTMLの整形
    jQuery.each(results, function() {
        html += '<div class="item ' + this.color + ' ' + this.type + '">';
        html += this.name;
        html += '</div>';
    });

    // HTMLに出力
    $('#items').empty().append(html);
}


// ページ読み込み時はすべてのアイテムを出力する
$(window).on('load', function(){
  outputResults(items);
});
              
            
!
999px

Console