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

188
Vistas
Is there any way that I can use variables while changing the the properties of document.getElemtentbyId(").style in JavaScript?

Is there any way that I can change the properties in document.getElementbyId.style with a variable that I received from the Parameters of the function?

function toggleFunction(elementId, property1, property2) {
    let element = document.getElementById(elementId)
    if (element.style.display == property1) {
        element.style.display = property2
    } else {
        element.style.display = property1
    }
}

The above code is not giving me the desired result. Is there a way that I can use variable values that I got from parameters for writing Properties?

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

0

Maybe I didn't catch what you need but its simple. Your code should work good if you use correct arguments for you func. But don't use "element" name twice. That is bad practise

const elementId = 'your-element-id'
const property1 = 'none'
const property2 = 'block'

const element = document.getElementById(elementId)
element.style.display = property1 // Set element's display = 'none'

The same way for another style properties:

element.style.width = '50%'
element.style.color = '#ff0000'
element.style.marginTop = '14px'

Read more -> HTML DOM Style Object

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