Pen Settings

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. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <form id="make_checkbox_select">
  <select name="category">
      <option value="My first category">My first category</option>
      <option value="My second category">My second category</option>
      <option value="My third category">My third category</option>
      <option value="My fourth category">My fourth category</option>
      <option value="My fifth category">My fifth category</option>
      <option value="My sixth category">My sixth category</option>
      <option value="My seventh category">My seventh category</option>
      <option value="My eighth category">My eighth category</option>
      <option value="My ninth category">My ninth category</option>
      <option value="My tenth category">My tenth category</option>
  </select>

  <input type="submit">
</form>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>

<script>
  $(function() {
    var mySelectCheckbox = new checkbox_select({
      selector: "#make_checkbox_select",
      selected_translation: 'selected',
      all_translation: 'All categories',
      not_found: 'Nothing found',
      // Event during initialization
      onApply: function(e) {
        alert("Custom Event: " + e.selected);
      }
    });
  });
</script>
            
          
!
            
              body {
    font-family: -apple-system,BlinkMacSystemFont,"Segoe UI",Helvetica,Arial,sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol";
    font-size: 14px;
    line-height: 1.5;
    color: #24292e;
    background-color: #fff;
}
input, select, button {
  font-family: inherit;
  font-size: inherit;
}

div.checkbox_select {
  width: 200px;
}
.checkbox_select_anchor {
  display: block;
  background-color: #EEEEEE;
  cursor: pointer;
  padding: 8px;
  position: relative;
}

.checkbox_select_anchor:hover {
  background-color: #CCCCCC !important;
}

.expanded .checkbox_select_anchor {
  background-color: #CCCCCC;
}

div.checkbox_select .select_input {
  width: 100%;
  cursor: pointer;
}

.checkbox_select_dropdown {
  display: none;
  background: whitesmoke;
}

.checkbox_select_dropdown.show {
  display: block;
}

.checkbox_select_dropdown ul {
  max-height: 150px;
  overflow-y: scroll;
  overflow-x: hidden;
  padding: 0;
  margin: 0;
  border: 1px solid #999;
  border-top: 0;
  border-bottom: 0;
}
.checkbox_select_dropdown ul li {
  list-style: none;
  position: relative;
  color: #666;
}
.checkbox_select_dropdown ul li label {
  position: relative;
  padding: 0 0 0 24px;
  display: block;
  cursor: pointer;
}
.checkbox_select_dropdown ul li label:hover {
  background: #cbcbcb;
  color: white;
}
.checkbox_select_dropdown ul li input:checked + label {
  background: #ccc;
  color: black;
}
.checkbox_select_dropdown ul li input {
  position: absolute;
  left: 0;
  z-index: 1;
  display: inline-block;
}
.checkbox_select_search {
  width: 200px;
}

.checkbox_select_submit {
  width: 100%;
  cursor: pointer;
}

.hide {
  display: none;
}

            
          
!
            
              var checkbox_select = function(params) {
  var that = this,
      $_native_form = $(params.selector),
      $_native_select = $_native_form.find("select"),

      // Variables
      selector = params.selector,
      select_name = $_native_select.attr("name").charAt(0).toUpperCase() + $_native_select.attr("name").substr(1),
      selected_translation = params.selected_translation ? params.selected_translation : "selected",
      all_translation = params.all_translation ? params.all_translation : "All " + select_name + "s",
      not_found_text = params.not_found ? params.not_found : "No " + select_name + "s found.",
      currently_selected = [],

      // Create the elements needed for the checkbox select
      $_parent_div = $("<div />").addClass("checkbox_select"),
      $_select_anchor = $("<a />").addClass("checkbox_select_anchor").text(select_name),
      $_search = $("<input />").addClass("checkbox_select_search"),
      $_submit = $("<input />").addClass("checkbox_select_submit").val("Apply").attr("type", "submit").data("selected", ""),
      $_dropdown_div = $("<div />").addClass("checkbox_select_dropdown"),
      $_not_found = $("<span />").addClass("not_found hide").text(not_found_text),
      $_ul = $("<ul />"),

      updateCurrentlySelected = function() {
        var selected = [];

        $_ul.find("input:checked").each(function() {
          selected.push($(this).val());
        });

        currently_selected = selected;

      if (selected.length == 0) {
        $_select_anchor.text(select_name);
      } else if (selected.length == 1) {
        $_select_anchor.text(selected[0] + " " + selected_translation);
      } else if (selected.length == $_ul.find("input[type=checkbox]").length) {
        $_select_anchor.text(all_translation);
      } else {
        $_select_anchor.text(selected.length + " " + selected_translation);
      }
    },
    // Template for the li, will be used in a loop.
    createItem = function(name, value) {
      var uID = "checkbox_select_" + select_name + "_" + name.replace(" ", "_"),
        $_li = $("<li />"),
        $_checkbox = $("<input />")
          .attr({
            name: name,
            id: uID,
            type: "checkbox",
            value: value
          })
          .click(function() {
            updateCurrentlySelected();
          }),
        $_label = $("<label />").attr("for", uID),
        $_name_span = $("<span />")
          .text(name)
          .prependTo($_label);

      return $_li.append($_checkbox.after($_label));
    },
    apply = function() {
      $_dropdown_div.toggleClass("show");
      $_parent_div.toggleClass("expanded");

      if (!$_parent_div.hasClass("expanded")) {
        // Only do the Apply event if its different
        if (currently_selected != $_submit.data("selected")) {
          $_submit.data("selected", currently_selected);

          that.onApply({
            selected: $_submit.data("selected")
          });
        }
      }
    };

  // Event of this instance
  that.onApply =
    typeof params.onApply == "function"
      ? params.onApply
      : function(e) {
          //e.selected is accessible
        };

  that.update = function() {
    $_ul.empty();
    $_native_select.find("option").each(function(i) {
      $_ul.append(
        createItem($(this).text(), $(this).val())
      );
    });

    updateCurrentlySelected();
  };

  that.check = function(checkbox_name, checked) {
    $_ul.find("input[type=checkbox]").each(function() {
      // If this elements name is equal to the one sent in the function
      if ($(this).attr("name") == checkbox_name) {
        // Apply the checked state to this checkbox
        $(this).attr("checked", checked ? checked : false);

        // Break out of each loop
        return false;
      }
    });

    updateCurrentlySelected();
  };

  // Build mark up before pushing into page
  $_dropdown_div.prepend($_search);
  $_dropdown_div.append($_ul);
  $_dropdown_div.append($_not_found);
  $_dropdown_div.append($_submit);
  $_dropdown_div.appendTo($_parent_div);
  $_select_anchor.prependTo($_parent_div);

  // Iterate through option elements
  that.update();

  // Events

  // Actual dropdown action
  $_select_anchor.click(function() {
    apply();
  });

  // Filters the checkboxes by search on keyup
  $_search.keyup(function() {
    var search = $(this)
      .val()
      .toLowerCase()
      .trim();

    if (search.length == 1) {
      $_ul.find("label").each(function() {
        if (
          $(this)
            .text()
            .toLowerCase()
            .charAt(0) == search.charAt(0)
        ) {
          if (
            $(this)
              .parent()
              .hasClass("hide")
          )
            $(this)
              .parent()
              .removeClass("hide");

          if (!$_not_found.hasClass("hide")) $_not_found.addClass("hide");
        } else {
          if (
            !$(this)
              .parent()
              .hasClass("hide")
          )
            $(this)
              .parent()
              .addClass("hide");

          if ($_not_found.hasClass("hide")) $_not_found.removeClass("hide");
        }
      });
    } else {
      // If it doesn't contain
      if ($_ul.text().toLowerCase().indexOf(search) == -1) {
        if ($_not_found.hasClass("hide")) $_not_found.removeClass("hide");
      } else {
        if (!$_not_found.hasClass("hide")) $_not_found.addClass("hide");
      }

      $_ul.find("label").each(function() {
        if ($(this).text().toLowerCase().indexOf(search) > -1) {
          if ($(this).parent().hasClass("hide"))
            $(this).parent().removeClass("hide");
        } else {
          if (!$(this).parent().hasClass("hide"))
            $(this).parent().addClass("hide");
        }
      });
    }
  });

  $_submit.click(function(e) {
    e.preventDefault();

    apply();
  });

  // Delete the original form submit
  $(params.selector).find("input[type=submit]").remove();

  // Put finalized markup into page.
  $_native_select.after($_parent_div);

  // Hide the original element
  $_native_select.hide();
};

            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console