Topic: Input initialization fails after loading a modal form
My modal fetches a form only when opened. Initializing the new text input throws a null reference error.
Which DOM element should be passed to the Input constructor?
Igor Przybysz
staff pro premium priority answered 2 weeks ago
Pass the enclosing .form-outline element to Input.getOrCreateInstance(), rather than the inner input. Locate that wrapper after the fetched form has actually been inserted into the modal, and check that the lookup returned an element before initializing it.
The input and its label should remain inside the wrapper, with the label's for attribute matching the input ID. If the error continues, please share the rendered wrapper HTML, the fetch/insertion callback, and the initialization line so we can reproduce the exact sequence.
const wrapper = modalBodyElement.querySelector('.form-outline');
if (wrapper) {
Input.getOrCreateInstance(wrapper);
}
https://mdbootstrap.com/docs/standard/forms/input-fields/
mike55 commented 2 weeks ago
That fixed the null reference. I was passing the input itself, and my lookup also ran before the fetched markup was inserted. Using its .form-outline wrapper inside the insertion callback works now, including when I reopen the modal. Thanks for clarifying the target element.
FREE CONSULTATION
Hire our experts to build a dedicated project. We'll analyze your business requirements, for free.
Answered
- User: Free
- Premium support: No
- Technology: MDB Standard
- MDB Version: MDB5 9.2.0
- Device: Desktop
- Browser: Chrome
- OS: Windows 11
- Provided sample code: No
- Provided link: No