Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

132
Vistas
using template literals with classlist attribute

javascript newbie here. I'm trying to implement template literals to change the second part of a variable name while calling the toggle method with the classlist attribute. Here's what I have:

bigBoxRight.classList.toggle("turnOff");

and this is what i'm trying to accomplish:

bigBox`${player}`.classList.toggle("turnOff");

Player can be both "Right" or "Left" and it's a string. Is this even possible?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Instead of trying to dynamically create variable names, you should get the element you are trying to toggle the class on first.

Assuming bigBoxRight or bigBoxLeft is the id of the element, you can use your template literals to get the element first:

var elem = document.querySelector(`#bigBox${player}`)

Then you can do:

elem.classList.toggle("turnOff");
about 4 years ago · Juan Pablo Isaza Denunciar

0

What I would do is to save another variable that will get the element you need based on your decision and then get the classlist for it

For example

const bigBoxRight = docemnt.querySelector...
const bigBoxLeft = docemnt.querySelector...

const elemRef = true* ? bigBoxRight : bigBoxLeft 

elemRef.classList.toggle("turnOff");

*-  Your logic for when you need the right or the left  bigbox element

Tim's solution might be cleaner if you know when you need the right element or the left

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda