I have defined following function:
function handleMessageDot(fun) {
domElement.classList.fun('hidden');
}
Now I want to use one of those:
handleMessageDot(add)
, handleMessageDot(remove)
.
I get remove is not defined
error. I assume that is because remove() and add() functions are classList methods.
I now I can do the following:
function handleMessageDot(domElement, fun) {
domElement.classList.fun('hidden');
}
but I don’t want to do that.
Is there a way to do ‘my’ way?
mgar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
0
When attempting to access the property of an object using the value of a separate variable you need to use array notation:
const handleMessageDot = (domElement, fun) => domElement.classList[fun]('hidden');
handleMessageDot(document.querySelector('.hidden'), 'remove');
.hidden {
display: none;
}
<div class="hidden">
Lorem ipsum
</div>
1