<div class="modal fade modal-s" id="staticBackdrop" data-bs-backdrop="static" data-bs-keyboard="false"
tabindex="-1" aria-labelledby="staticBackdropLabel" aria-hidden="true">
content..............
</div>
by using javascript this div is going display none and block
i want when this div is display block i wiil add a class list to another div and when the none i will remove the class
i have tried this but didnt work the div with id ‘over-lap’ i want to add and remove class from that
document.addEventListener('DOMContentLoaded', function() {
var mod = document.querySelector('.modal-s');
var overLapDiv = document.getElementById('over-lap');
if (mod.style.display == 'none') {
overLapDiv.classList.remove('overlap-div'); // Fixed typo
} else if (mod.style.display == 'block'){
overLapDiv.classList.add('overlap-div');
}
});
other logic i tried
<body>
<div class="modal-s show">...</div>
<div id="over-lap"></div>
<script>
document.addEventListener('DOMContentLoaded', function() {
var mod = document.querySelector('.modal-s');
var overLapDiv = document.getElementById('over-lap');
if (mod.classList.contains('show')) {
overLapDiv.classList.add('overlap-div');
} else {
overLapDiv.classList.remove('overlap-div');
}
});
</script>
</body>
nothing worked
2
Answers
This happens because
mod.style.display
is empty. You need to usewindow.getComputedStyle(mod, null).display
. Example: