I have a button:
<button type="button" id="errorLog" class="btn btn-secondary btn-lg btn-block">Download</button>
that is only visible if a condition is met. I'm trying to add an event listener to fire some JavaScript based on the visibility of the button. In my research I see that I could use a MutationObserver
but for some reason I get an error of:
'observe' on 'MutationObserver': parameter 1 is not of type 'Node'
After searching I did see:
- 'observe' on 'MutationObserver': parameter 1 is not of type 'Node'
- Uncaught TypeError: Failed to execute 'observe' on 'MutationObserver': parameter 1 is not of type 'Node'
- MutationObserver div change src
but for some reason when I code the MutationObserver
as:
let errorFile = document.getElementById('errorLog')
var observer = new MutationObserver(function() {
if (errorFile.style.display == 'display') {
console.log('foobar')
}
})
const config = {
attributes: true,
childList: true,
characterData: true
}
observer.observe(errorFile, config)
I get the error of Uncaught TypeError: Failed to execute 'observe' on 'MutationObserver': parameter 1 is not of type 'Node'
so I tried a conditional of:
let errorFile = document.getElementById('errorLog')
if (errorFile != null) {
var observer = new MutationObserver(function() {
if (errorFile.style.display == 'display') {
console.log('foobar')
errorLog.addEventListener('click', (e) => {
e.preventDefault
console.log('fired click')
})
}
})
const config = {
attributes: true,
childList: true,
characterData: true
}
observer.observe(errorFile, config)
}
I've also referenced:
- JS: event listener for when element becomes visible?
- Must an element be visible in order to “load” event listener to work?
- Showing and hiding element, making my code more useful
but it doesn't even fire. How can I fire JavaScript based on the visibility of a button?