Skip to content

Commit

Permalink
Support ingesting elements in multiple forms
Browse files Browse the repository at this point in the history
All of the following will work:
new Dropdownizer("#foo");
new Dropdownizer(["#foo2", "#foo3"]);
new Dropdownizer(document.querySelector("#foo4"));
new Dropdownizer(document.querySelectorAll(".foos"));
  • Loading branch information
grafluxe committed Nov 22, 2017
1 parent ba93ea6 commit b778cc3
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions src/Dropdownizer.js
Expand Up @@ -9,12 +9,28 @@ const CLASS_NAME = "dropdownizer";
class Dropdownizer{

constructor(el) {
if (el.nodeType) {
new Dropdownize(el).change(evt => this._onChange(evt));
} else {
el.forEach(element => {
new Dropdownize(element).change(evt => this._onChange(evt));
});
try {
if (typeof el === "string") {
el = document.querySelector(el);
} else if(el instanceof Array) {
el = el.map(element => document.querySelector(element));
}

if (el.nodeType) {
new Dropdownize(el).change(this._onChangeProxy.bind(this));
} else {
el.forEach(element => {
new Dropdownize(element).change(this._onChangeProxy.bind(this));
});
}
} catch (err) {
throw new Error("No such element exists");
}
}

_onChangeProxy(evt) {
if(this._onChange) {
this._onChange(evt);
}
}

Expand Down

0 comments on commit b778cc3

Please sign in to comment.