bootstrap: TypeError Cannot read properties of undefined (reading 'backdrop') - bootstrap v5.2.1 modal
Discussed in https://github.com/twbs/bootstrap/discussions/37304
<div type='discussions-op-text'>Originally posted by rohith2810 October 12, 2022 While using bootstrap modal I am facing this error ( TypeError Cannot read properties of undefined (reading ‘backdrop’) ) , which is tracked by our bug tracker, but I am unable to recreate this error. I tried recreating error with various browsers, browser versions, OS. This issue is not showing up on all browsers.
Bootstrap version used - v5.2.1
I initialized modal using JavaScript
if (bootstrap == undefined) {
document.getElementById("browser-not-supported-container").classList.remove("d-none");
} else {
modal1 = new bootstrap.Modal(document.getElementById('modal1'));
modal2 = new bootstrap.Modal(document.getElementById('modal2'));
modal3 = new bootstrap.Modal(document.getElementById('modal3'));
}
Html code for these modals
<!-- Modal 1-->
<div class="modal fade text-start" data-bs-backdrop="static" data-bs-keyboard="false" id="modal1" tabindex="-1" role="dialog">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<!-- Header -->
</div>
<div class="modal-body">
<!-- Body -->
</div>
</div>
</div>
</div>
<!-- Modal 2-->
<div class="modal fade text-start" id="modal2" tabindex="-1" role="dialog">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-body text-center">
<!-- Body -->
</div>
</div>
</div>
</div>
<!-- Modal 3-->
<div class="modal fade text-start" id="modal3" tabindex="-1" role="dialog">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-body text-center">
<!-- Body -->
</div>
</div>
</div>
</div>
The exact error is:
TypeError Cannot read properties of undefined (reading 'backdrop')
https://example.com/:lineNo3:colNo3 Ni._initializeBackDrop
https://example.com/:lineNo2:colNo2 new Ni
https://example.com/:lineNo1:colNo1 HTMLDocument.<anonymous>
Note: lineNo1 is where modal2 is initializing
I also tried with modal options but error is still persisting
modal1 = new bootstrap.Modal(document.getElementById('modal1'), { backdrop: "static", keyboard: false });
modal2 = new bootstrap.Modal(document.getElementById('modal2'), { backdrop: "static", keyboard: false });
modal3 = new bootstrap.Modal(document.getElementById('modal3'), { backdrop: "static", keyboard: false });
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 9
- Comments: 16 (3 by maintainers)
Commits related to this issue
- fix undefined is not an object (evaluating 'this._config.backdrop') see https://github.com/twbs/bootstrap/issues/37305 for context if the element failed to initialize (this._element never being se... — committed to lekoala/bootstrap by lekoala a year ago
- fix: https://github.com/twbs/bootstrap/issues/37305 — committed to weseek/growi by miya 5 months ago
My first progress on this error was removing data-bs-toggle=“modal” since im already showing/hiding modals via a click handler.
Upon further examination, this error is being shown to me because of data-bs-target being wrong because on some pages im going modal to modal and on other pages im not. dont be like me.
So what do you propose to do?
For sure, please try to refactor this
modal1 = new bootstrap.Modal(document.getElementById('modal1'));to
modal1 = bootstrap.Modal.getOrCreateInstance('#modal1');It will save you from several possible mistakes
This is happening due to an invalid / or non existing DOM element being passed. I discovered this today by mistakenly passing the wrong selector.
“myModal” !== “#myModal” when using the getOrCreateInstance method.
What happens is it gets to base-component js and tries to set up
this._elementandthis._config, but fails. Then modal js kicks in and attempts to build the modal and backdrop, but thethis._elementis undefined. You can see here thethiscontext is missing the config object as well as the rest of the keys.Hopes this helps someone 😃
Such a simple oversight but thank you for saving me time!
Easy to overlook but for anyone else seeing this, it says in the docs that
data-bs-toggle="modal"is for cases where JS is not used.@rohith2810 Make sure document.getElementById(‘modal#’) is not returning null in else block
Thanks, it works for me!