I'm trying to add and remove a class when a dropdownmenu is being toggled on/off. However, when i run this code locally, i get this error:
Uncaught TypeError: Failed to execute 'observe' on 'MutationObserver': parameter 1 is not of type 'Node'
I've tried to run the exact same code on JSfiddle, which works just fine. What could this be caused by?
var button = document.getElementById("dropdownMenuButton");
var element = document.getElementById("dropText");
window.MutationObserver = window.MutationObserver
|| window.WebKitMutationObserver
|| window.MozMutationObserver;
var target = document.getElementById("dropdownMenuButton");
observer = new MutationObserver(function(mutation) {
if(button.getAttribute("aria-expanded") == 'true') {
element.classList.add("myEffect");
}else{
element.classList.remove("myEffect");
}
}),
config = {
attributes: true
};
observer.observe(target, config);
.myEffect {
position: absolute;
left: 11% !important;
top: -35% !important;
background-color: white;
color: #3faf8f;
}
<div class="dropdown" id="dropDownDiv">
<button class="btn btn-secondary dropdown-toggle dropdownButton" type="button"
id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"
onclick="inputEffect()">
<span class="dropdownText" id="dropText">Vælg mærke</span><i
class="fas fa-chevron-down fasDropDown"></i>
</button>
<div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
<a class="dropdown-item" href="#">Action</a>
<a class="dropdown-item" href="#">Another action</a>
<a class="dropdown-item" href="#">Something else here</a>
</div>
</div>