After you've cloned the repo you will need to add the library to your page. In the build/js
folder use
one of the two DropKick files given. One has a version number in the file name and the other is a version
number-less version. You will also need to grab the css from build/css
and load it on the page.
Once those files are imported into the page you can call DropKick on any HTMLSelectElement:
new Dropkick( HTMLSelectElement, Options );
or new Dropkick( "ID", Options );
. This returns the dropkick
object to you. It may be useful for you to store this in a var to reference later.
If you're using jQuery you can do this instead:
$('#select').dropkick( Options );
Parameters
select
options
opts See list of properties you can pass in heresel
elem HTMLSelect Element being passed.
Examples
// Pure JS
var select = new Dropkick("#select");
// jQuery
$("#select").dropkick();
Returns object DropKick Object for that select. You can call your methods on this if stored in a var
Whether the form is currently disabled or not
Properties
disabled
boolean
Examples
var select = new Dropkick("#select");
select.disabled;
The form associated with the select
Properties
form
node
Examples
var select = new Dropkick("#select");
select.form;
The number of options in the select
Properties
length
integer
Examples
var select = new Dropkick("#select");
select.length;
If this select is a multi-select
Properties
multiple
boolean
Examples
var select = new Dropkick("#select");
select.multiple;
An array of Dropkick options
Properties
options
array
Examples
var select = new Dropkick("#select");
select.options;
An index of the first selected option
Properties
selectedIndex
integer
Examples
var select = new Dropkick("#select");
select.selectedIndex;
An array of selected Dropkick options
Properties
selectedOptions
array
Examples
var select = new Dropkick("#select");
select.selectedOptions;
The current value of the select
Properties
value
string
Examples
var select = new Dropkick("#select");
select.value;
Adds an element to the select. This option will not only add it to the original select, but create a Dropkick option and add it to the Dropkick select.
Parameters
elem
string HTMLOptionElementbefore
Node/Integer HTMLOptionElement/Index of Element
Examples
var select = new Dropkick("#select");
select.add("New option", 5);
Selects an option in the list at the desired index (negative numbers select from the end).
Parameters
index
Integer Index of element (positive or negative)
Examples
var select = new Dropkick("#select");
select.item(4); //returns DOM node of index
Returns Node The DK option from the list, or null if not found
Removes the option (from both the select and Dropkick) at the given index.
Parameters
index
Integer Index of element (positive or negative)
Examples
var select = new Dropkick("#select");
select.remove(4);
Closes the DK dropdown
Examples
var select = new Dropkick("#select");
select.close(); //closes dk dropdown
Opens the DK dropdown
Examples
var select = new Dropkick("#select");
select.open(); //Opens the dk dropdown
Disables or enables an option; if only a boolean is passed (or nothing), then the entire Dropkick will be disabled or enabled.
Parameters
elem
Integer The element or index to disabledisabled
Boolean Value of disabled
Examples
var select = new Dropkick("#select");
// To disable the entire select
select.disable();
// To disable just an option with an index
select.disable(4, true);
// To re-enable the entire select
select.disable(false);
// To re-enable just an option with an index
select.disable(4, false);
Hides or shows an option.
Parameters
elem
Integer The element or index to hidehidden
Boolean Whether or not to hide the element
Examples
var select = new Dropkick("#select");
// To hide an option with an index
select.hide(4, true);
// To make an option visible with an index
select.hide(4, false);
Selects an option from the list
Parameters
Examples
var elm = new Dropkick("#select");
// Select by index
elm.select(4); //selects & returns 5th item in the list
// Select by value
elm.select("AL"); // selects & returns option with the value "AL"
Returns Node The selected element
Selects a single option from the list and scrolls to it (if the select is open or on multi-selects). Useful for selecting an option after a search by the user. Important to note: this doesn't close the dropdown when selecting. It keeps the dropdown open and scrolls to proper position.
Parameters
elem
Integer The element or index to selectdisabled
Boolean Selects disabled options
Examples
var select = new Dropkick("#select");
select.selectOne(4);
Returns Node The selected element
Finds all options who's text matches a pattern (strict, partial, or fuzzy)
"strict"
- The search string matches exactly from the beginning of the
option's text value (case insensitive).
"partial"
- The search string matches part of the option's text value
(case insensitive).
"fuzzy"
- The search string matches the characters in the given order (not
exclusively). The strongest match is selected first. (case insensitive).
Parameters
pattern
mode
Integer How to search; "strict", "partial", or "fuzzy"string
String The string to search for
Returns Boolean An Array of matched elements
Brings focus to the proper DK element
Examples
var select = new Dropkick("#select");
$("#some_elm").on("click", function() {
select.focus();
});
Resets the Dropkick and select to it's original selected options; if clear
is true
,
It will select the first option by default (or no options for multi-selects).
Parameters
clear
Boolean Defaults to first option if true
Examples
var select = new Dropkick("#select");
// Reset to originally `selected` option
select.reset();
// Reset to first option in select
select.reset(true);
Rebuilds the DK Object (use if HTMLSelectElement has changed)
Examples
var select = new Dropkick("#select");
//... [change original select] ...
select.refresh();
Removes the DK Object from the cache and the element from the DOM
Examples
var select = new Dropkick("#select");
select.dispose();
Called once after the DK element is inserted into the DOM.
The value of this
is the Dropkick object itself.
Whether or not you would like Dropkick to render on mobile devices.
Properties
mobile
boolean
Called whenever the value of the Dropkick select changes (by user action or through the API).
The value of this
is the Dropkick object itself.
Called whenever the Dropkick select is opened. The value of this
is the Dropkick object itself.
Called whenever the Dropkick select is closed. The value of this
is the Dropkick object itself.
"strict"
- The search string matches exactly from the beginning of the option's text value (case insensitive).
"partial"
- The search string matches part of the option's text value (case insensitive).
"fuzzy"
- The search string matches the characters in the given order (not exclusively).
The strongest match is selected first. (case insensitive).
Bubble up the custom change event attached to Dropkick to the original element (select).