From b778cc306ab0bc744e3d697be0233e749d5e8d90 Mon Sep 17 00:00:00 2001 From: Leandro Silva Date: Sat, 18 Nov 2017 03:01:51 -0500 Subject: [PATCH] Support ingesting elements in multiple forms All of the following will work: new Dropdownizer("#foo"); new Dropdownizer(["#foo2", "#foo3"]); new Dropdownizer(document.querySelector("#foo4")); new Dropdownizer(document.querySelectorAll(".foos")); --- src/Dropdownizer.js | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/src/Dropdownizer.js b/src/Dropdownizer.js index 65d6769..2dd7d41 100644 --- a/src/Dropdownizer.js +++ b/src/Dropdownizer.js @@ -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); } }