M.FormSelect.getInstance returns null for dynamically added select
0 reputation · 28 Feb 2022, 14:43 UTC
0 reputation · 28 Feb 2022, 14:43 UTC
A <select> added to the DOM after the initial M.AutoInit() call should be enhanced like other Materialize selects: wrapped in .select-wrapper and returnable from M.FormSelect.getInstance().
Assuming MaterializeCSS v1.0.0, jQuery-based initialization examples do not apply. M.AutoInit() enhances only elements present when it runs, so later inserts remain native unless initialized imperatively. Whether M.FormSelect.init() is idempotent, or whether repeated calls create duplicate wrappers or dropdowns, is not clearly documented. Behavior may also differ between CDN and npm builds.
M.FormSelect.init() be called explicitly on each <select> appended after startup?.select-wrapper markup?37775 reputation · 28 Feb 2022, 22:16 UTC
Yes. In MaterializeCSS v1.0.0, M.AutoInit() enhances only the <select> elements present when it runs. A select appended to the DOM later remains a native element, so M.FormSelect.getInstance(el) returns null until you call M.FormSelect.init(el) on that specific element after insertion.
The null return means no M.FormSelect instance is attached to the element. Because M.AutoInit() does not observe later DOM changes, dynamically added selects are never wrapped in .select-wrapper and are not registered in the component's instance map.
M.FormSelect.init(element) accepts a single element, a NodeList, or a selector string and returns an array of instances.init() again on the same <select> without destroying the prior instance creates duplicate .select-wrapper markup and multiple dropdown overlays.instance.destroy() removes the wrapper and listeners but leaves the original <select> in the DOM.M.FormSelect.getInstance(el) returns null after destroy() or when the element was never initialized.<select> to the DOM.M.FormSelect.init(newSelect) immediately after insertion, targeting only the newly added element.M.FormSelect.getInstance(newSelect) to obtain the instance.const inst = M.FormSelect.getInstance(el); if (inst) inst.destroy(); Then call init(el) again.const select = document.createElement('select');
select.name = 'example';
container.appendChild(select);
const instances = M.FormSelect.init(select);
const instance = instances[0]; // equivalent to M.FormSelect.getInstance(select)
M.AutoInit().<select> and confirm M.FormSelect.getInstance(select) is null before init.M.FormSelect.init(select) and confirm one .select-wrapper appears and getInstance returns an object.init(select) again without destroy() and confirm duplicate wrappers appear.instance.destroy(), then init(select) again, and confirm only one wrapper remains.If your page replaces selects in place rather than appending them, confirm whether you call destroy() before re-initializing; that is the only detail that changes whether duplicate wrappers will appear.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.