1

Instead of doing this: document.querySelector(".class1").addEventListener('wheel', this.doSomething)

I want to do something like this: document.querySelector(this.state.currentClass).addEventListener('wheel', this.doSomething)

Is it possible? The other workaround I can think of is like this:

let temp = "." + this.state.currentClass;
document.querySelector(temp).addEventListener('wheel', this.doSomething);

Is there a standard or more optimal way of doing this?

1 Answer 1

2

you could use string interpolation and just do:

document.querySelector(`.${this.state.currentClass}`)

string interpolation works like this:

anything in the string is interpreted as a string EXCEPT anything in ${} is read as javascript and converted to a string

const hello = 'goodbye' e.g. hello{hello} // returns hellogoodbye

I would recommend doing it the way I've shown and anytime you want to append something, just update the className in state

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

2 Comments

What if I have to append something to it? Actually my className is numerical so I need to append a string to it. Like from 0 I want to make it .0class for querySelector()
@AkhileshSingla I have updated, let me know if you still need help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.