I found a workaround for this issue.
Once the modal has been hidden bootstrap data still remains on it. To prevent that I had the following:
$('#myModal').modal('show'); //display something
//...
// if you don't want to lose the reference to previous backdrop
$('#myModal').modal('hide');
$('#myModal').data('bs.modal',null); // this clears the BS modal data
//...
// now works as you would expect
$('#myModal').modal({backdrop:'static', keyboard:false});
Answer from Daniele Piccioni on Stack OverflowI found a workaround for this issue.
Once the modal has been hidden bootstrap data still remains on it. To prevent that I had the following:
$('#myModal').modal('show'); //display something
//...
// if you don't want to lose the reference to previous backdrop
$('#myModal').modal('hide');
$('#myModal').data('bs.modal',null); // this clears the BS modal data
//...
// now works as you would expect
$('#myModal').modal({backdrop:'static', keyboard:false});
I had the same problem with Bootstrap 4.1.1 and it only worked when I added the data attributes to the html
<div class="modal fade show" id="myModal" tabindex="-1" role="dialog"
style="display: block;" data-keyboard="false" data-backdrop="static">
...
data-backdrop is not working on livewire.
Missing modal static backdrop animation
data-bs-backdrop and data-bs-keyboard not functioning on v5.0.1
TypeError Cannot read properties of undefined (reading 'backdrop') - bootstrap v5.2.1 modal
As per Bootstrap docs:
Clicking on the modal “backdrop” will automatically close the modal.
When backdrop is set to static, the modal will not close when clicking outside it.
Via JS:
$('#myModal').modal({
backdrop: 'static',
keyboard: false
})
$('#myModal').modal({backdrop: 'static', keyboard: false}, 'show');
Via HTML:
<a data-controls-modal="your_div_id" data-backdrop="static" data-keyboard="false" href="#">
Works too, data-backdrop="static" to click out and data-keyboard="false" to Esc in your div modal:
<div id="idModal" class="modal hide" data-backdrop="static" data-keyboard="false">
I'm using Vue 3 and Bootstrap 5.2.3. I'm trying to show a Modal that I had created and received the exact same error as you did. Here's what the problem was.
Vue has various lifecycle events that can be plugged into to perform actions. Initially I was doing this work in the setup stage:
<script>
import { Modal } from 'bootstrap'
export default {
name: 'MyModal',
setup() { <-- Too early
const myModal = new Modal(document.getElementById('my-modal'), {})
myModal.show()
}
}
</script>
The problem is that the Modal has not been loaded into the DOM at this stage and therefore is undefined when the getElementById action is performed. The solution was to wait until the DOM is loaded by using the mounted stage like so:
<script>
import { Modal } from 'bootstrap'
export default {
name: 'MyModal',
mounted() { <-- Just right
const myModal = new Modal(document.getElementById('my-modal'), {})
myModal.show()
}
}
</script>
If you're not using Vue the answer should be similar. Perform the getElementById action in an event callback function that fires only after the DOM is loaded for instance DOMContentLoaded
When I had this bug, the error was that I was missing the pound sign/hashsign/octothorpe in front of the modal ID in the data-bs-target attribute. (That is, I had data-bs-target="my-modal" instead of data-bs-target="#my-modal".
Set the modal's backdrop to static. The modal component has a prop of backdrop, set that to backdrop="static"
<div>
<Modal show={this.state.show} onHide={this.handleClose} backdrop="static">
<Modal.Header>
<Modal.Title>Change Password</Modal.Title>
</Modal.Header>
<Modal.Body>
<form className="form-horizontal" style={{margin:0}}>
<div className='password-heading'>This is the first time you have logged in.</div>
<div className='password-heading'>Please set a new password for your account.</div>
<br/>
<label className="password">Password
<input type={this.state.type} className="password__input" onChange={this.passwordStrength}/>
<span className="password__show" onClick={this.showHide}>{this.state.type === 'input' ? 'Hide' : 'Show'}</span>
<span className="password__strength" data-score={this.state.score}>
<div className="strength_string">{this.state.strength}</div>
</span>
</label>
<br/>
<label className="password">Confirm Password
<input type={this.state.type} className="password__input" onChange={this.passwordStrength}/>
</label>
</form>
<br/>
</Modal.Body>
<Modal.Footer>
<Button onClick={this.submitPassword} disabled={this.state.isDisabled}>Submit</Button>
</Modal.Footer>
</Modal>
</div>
From the documentation:
Specify 'static' for a backdrop that doesn't trigger an "onHide" when clicked. react-bootstrap
Remove onHide from the Modal onHide={this.handleClose}