Skip to content

Commit

Permalink
Update query-selection logic
Browse files Browse the repository at this point in the history
Remove support for array ingestion and update string binding to
query-select all.
  • Loading branch information
grafluxe committed Nov 25, 2017
1 parent 653b2d1 commit a761150
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 22 deletions.
14 changes: 4 additions & 10 deletions README.md
Expand Up @@ -56,22 +56,16 @@ See the [documentation](http://grafluxe.com/o/doc/dropdownizer/Dropdownizer.html

## Examples

Pass in a `string` to quickly bind to the *first found* element.
Pass in a `string` to bind to the matching element(s).

```
new Dropdownizer("#my-dd");
new Dropdownizer("select");
```

Pass in an `array` to quickly bind to the *first found* elements.
Pass in an `DOM element` to bind to the matching element(s).

```
new Dropdownizer(["#my-dd", ".dropdown"]);
```

Pass in an `DOM element` to bind to the returned element(s).

```
new Dropdownizer(document.querySelectorAll("select"));
new Dropdownizer(document.querySelector("select"));
```

Use the `change` method.
Expand Down
2 changes: 1 addition & 1 deletion example/index.html
Expand Up @@ -39,7 +39,7 @@ <h1>Dropdownizer Examples</h1>
</section>
<script src="../dist/Dropdownizer.min.js"></script>
<script>
new Dropdownizer(document.querySelectorAll("select")).change(evt => {
new Dropdownizer("select").change(evt => {
document.querySelector("#selection").innerHTML = evt.data.label || evt.data.value;
});
</script>
Expand Down
24 changes: 13 additions & 11 deletions src/Dropdownizer.js
Expand Up @@ -10,24 +10,26 @@ class Dropdownizer {
let dds = [];

if (typeof el === "string") {
el = document.querySelector(el);
} else if(el instanceof Array) {
el = el.map(element => document.querySelector(element));
el = document.querySelectorAll(el);
}

if (!el) {
throw new Error("No such element exists.");
}

if (el.nodeType) {
dds.push(new Dropdownize(el));
} else {
el.forEach(element => {
dds.push(new Dropdownize(element));
});
}
try {
if (el.nodeType) {
dds.push(new Dropdownize(el));
} else {
el.forEach(element => {
dds.push(new Dropdownize(element));
});
}

this._dropdowns = Object.freeze(dds);
this._dropdowns = Object.freeze(dds);
} catch (err) {
throw new TypeError("Unexpected argument.");
}
}

selectItem(index) {
Expand Down

0 comments on commit a761150

Please sign in to comment.