A CSS selector string or a DOM Element to wait for removal.
Optional timeout in milliseconds. Defaults to 5000ms.
Promise that resolves when the element is removed, rejects on timeout.
This function uses a MutationObserver
to monitor changes in the DOM and checks if the specified element is removed. If the element is not found within the specified timeout, it rejects with an error.
// ES Module
import { waitForElementRemoved } from '@bnidev/js-utils'
// CommonJS
const { waitForElementRemoved } = require('@bnidev/js-utils')
// Wait for an element with ID 'myElement' to be removed
waitForElementRemoved('#myElement')
.then(() => {
console.log('Element was removed from the DOM')
})
.catch((error) => {
console.error('Error:', error)
})
// Wait for a specific element to be removed
const myElement = document.getElementById('myElement')
waitForElementRemoved(myElement)
.then(() => {
console.log('Element was removed from the DOM')
})
.catch((error) => {
console.error('Error:', error)
})
Waits for an element matching the selector (or the given element) to be removed from the DOM.