1

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?

0

1 Answer 1

2

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>

Sign up to request clarification or add additional context in comments.

1 Comment

That did a job. Thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.